Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/65890150/a-complete-dataframe-to-a-string-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...