A complete dataframe to a string in R

I have this data.frame in R;

input output aux

aa bb cc

a1 b1 c1

a2 b2 c2

I need this string

"input;output;aux#aa;bb;cc;#a1;b1;c1#a2;b2;c2"

Thanks!

Upvotes: 1

Views: 52

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193537

Another alternative is to just use write.table. The following gets you close:

capture.output(write.table(df1, sep = ";", quote = FALSE, eol = "#", row.names = FALSE))
## [1] "input;output;aux#aa;bb;cc#a1;b1;c1#a2;b2;c2#"

Use sub("#$", "", ...) to remove the last "#" in the output.

Upvotes: 0

akrun
akrun

Reputation: 887223

We can use paste

paste(paste(names(df1), collapse = ";"),
  do.call(paste, c(df1, sep = ";", collapse="#")), sep="#")

-output

#[1] "input;output;aux#aa;bb;cc#a1;b1;c1#a2;b2;c2"

Or using capture.output

paste(gsub("\\s+", ";", gsub("^\\d+\\s+", "", 
       trimws(capture.output(df1)))), collapse="#")

data

df1 <- structure(list(input = c("aa", "a1", "a2"), output = c("bb", 
"b1", "b2"), aux = c("cc", "c1", "c2")), class = "data.frame",
row.names = c(NA, 
-3L))

Upvotes: 0

Related Questions