简体   繁体   中英

In R how to combine multiple string columns in an R dataframe into one list column

I'm trying to combine multiple string columns in a data frame into a single list with multiple elements.

Here is some sample data:

Df = 
Note1   Note2   Note3   OtherStuff
“do”    na  “re”    54
“fa”    “so”    “ti”    na
“mi”    “do”    na  10

What I want is:

DF = 
Note1   Note2   Note3   OtherStuff  Notes1_3
“do”    na  “re”    54      c(“do”, na, “re”)
“fa”    “so”    “ti”    na      c(“fa”, “so”, “ti”)
“mi”    “do”    na  10      c(“mi”, “do, na)

I tried the code below as suggested in another Stackoverflow Q&A:

DF %>%
  mutate(Notes1_3 = mapply(c, Note1, Note2, Note3)

This resulted in Notes1_3 equal to NULL for all the rows in the dataframe. This looks like something that should be easy to do and I'm just missing some detail.

If you want Notes1_3 to be a list column as in the title, you just need to make it into a list, not a vector:

df <- structure(list(Note1 = c("“do”", "“fa”", "“mi”"), 
    Note2 = c("na", "“so”", "“do”"), Note3 = c("“re”", 
    "“ti”", "na"), OtherStuff = c("54", "na", "10")), class = "data.frame", row.names = c(NA, 
-3L))

df
  Note1 Note2 Note3 OtherStuff
1  “do”    na  “re”         54
2  “fa”  “so”  “ti”         na
3  “mi”  “do”    na         10

df2 <- df %>%
    mutate(Notes1_3 = list(Note1, Note2, Note3))

df2
  Note1 Note2 Note3 OtherStuff         Notes1_3
1  “do”    na  “re”         54 “do”, “fa”, “mi”
2  “fa”  “so”  “ti”         na   na, “so”, “do”
3  “mi”  “do”    na         10   “re”, “ti”, na

If we look specifically at that column, we see it's actually a list of vectors as desired:

df2$Notes1_3
[[1]]
[1] "“do”" "“fa”" "“mi”"

[[2]]
[1] "na"   "“so”" "“do”"

[[3]]
[1] "“re”" "“ti”" "na"  

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