Is there a way to unnest list columns in R to remove hierarchy and retain the individuals components in a vector? I tried unnest function from tidyr but I get an error
library(dplyr)
library(tidyr)
mydf <- tibble(
a1 = c(20, 21, 23, 45),
a2 = list(c("Male", "Female"), c("Yes", "No"),
c("Secondary", "Primary", "Tertiary"),
c("13-14", "15-16", "17 - 18"))
)
#---- Desired output
mydf1 <- tibble(
a1 = c(20, 21, 23, 45),
a2 = c("Male, Female", "Yes, No",
"Secondary, Primary, Tertiary",
"13-14, 15-16, 17 - 18"))
#--- Trial
mydf %>%
mutate(a3 = unnest(a2))
CodePudding user response:
I'd prefer sapply
with toString
:
mydf$a3 <- sapply(mydf$a2, toString)
And now:
> mydf
# A tibble: 4 x 3
a1 a2 a3
<dbl> <list> <chr>
1 20 <chr [2]> Male, Female
2 21 <chr [2]> Yes, No
3 23 <chr [3]> Secondary, Primary, Tertiary
4 45 <chr [3]> 13-14, 15-16, 17 - 18
>
toString
joins the elements into one string and separates them by ,
(comma space).
CodePudding user response:
This will work:
mydf %>%
unnest_wider(a2) %>% # not inside mutate and also '_wider' to get separate columns
unite(col = "a2", -a1, sep = ", ", na.rm = TRUE) #and now unite those separate cols
CodePudding user response:
Using map
library(dplyr)
library(purrr)
mydf %>%
mutate(a2 = map_chr(a2, toString))
# A tibble: 4 × 2
a1 a2
<dbl> <chr>
1 20 Male, Female
2 21 Yes, No
3 23 Secondary, Primary, Tertiary
4 45 13-14, 15-16, 17 - 18