简体   繁体   中英

how to concatenate data.frame into a single column?

I have created a mock up data like,

d1<-structure(list(`1` = c(10L, 15L, 20L), `2` = c(20L, NA, 15L), 
`3` = c(30L, 25L, NA)), .Names = c("1", "2", "3"),
  class = "data.frame",     row.names = c(NA, -3L))

Now I want to convert like,

d2<-structure(list(Name = structure(1:3, .Label = c("1,10|2,20|3,30",
 "1,15|2,0|3,25", "1,20|2,15|3,0"), class = "factor")),
.Names = "Name",  class     = "data.frame", row.names = c(NA, -3L))

How can I do it?

Thanks in advance.

Try

d1N <- d1
d1N[is.na(d1N)] <- 0
d1N[] <- paste(col(d1N), as.matrix(d1N), sep=',')

data.frame(Name=do.call(paste, c(d1N, sep='|')))
#           Name
#1 1,10|2,20|3,30
#2  1,15|2,0|3,25
#3  1,20|2,15|3,0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM