1

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!

2 Answers 2

0

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)) 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.