简体   繁体   中英

Convert data frame to one cell vector(s)

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.

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