Answers for "unpack list elements in dataframe R"

R
0

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)
Posted by: Guest on August-24-2020

Browse Popular Code Answers by Language