I would like to convert the columns from a dataframe into one cell.
Currently I am getting the following:
beruf <- c(" 2", " 3", " 5", NA, "aa", "bb", "cc", NA)
contact <- c(" 2", NA, NA, NA, "aa", NA, "ccda", NA)
beruf <- as.vector(as.matrix(beruf))
contact <- as.vector(as.matrix(contact))
# append to data frame
df.buffer <- data.frame(as(beruf, "character"), as(contact, "character"))
My Output is like that:
However, I would like to get the following:
Any suggestions how to get the desired output?
I appreciate your replies!
You can try in base
:
sapply(df.buffer, paste, collapse = ",")
or a little longer in data.table
:
setDT(df.buffer)[,lapply(.SD, paste, collapse = ",")]
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.