unpack list elements in dataframe R code example
Example: unpack list elements in dataframe R
library(tidyr)
df <- tibble('A' = c('id1', 'id2', 'id3'),
'B' = list(list('carb' = 'spaghetti',
'protein' = 'meatballs',
'sauce' = 'marinara')),
'C' = list(list('John', 'Paul', 'Ringo')))
#unnests so list names are new cols
df_wider <- df %>% unnest_wider(B)
#Just unnests sauce from nested list B
df_sauce <- df %>% hoist(B, sauce = 'sauce')
#unnest so list items are broken into rows
df_longer <- df %>% unnest_longer(C)