Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to extract all list elements from a dataframe column into individual columns

Tags:

r

purrr

After using purrr:map along a time series list column, I end up with the results in a tibble list (I may have some terminology wrong here, but hopefully the example will clear things up). Is it possible to extract each column in the resulting list as a data frame column without specifying each list element name?

Example data:

tmp <- tibble(col1 = c("A1", "A2") , 
              col2 = c("B1", "B2"), 
              col3 = list(
                  list(x = TRUE, b = list(data.frame(y1=c(1,2,3), y2=c(4,5,6)))), 
                  list(x = FALSE, b = list(data.frame(y1=c(1,2,3), y2=c(4,5,6))))))

Required output (but without having to type each column - in reality I have a lot more):

tmp %>% mutate(x = map(tmp$col3, "x")[[1]],
               b = map(tmp$col3, "b")[[1]])

Edit: I have since realised my "manual solution" above is wrong.. I am not sure how to even extract b manually, but for x, it should have been:

tmp %>% mutate(x = map_lgl(col3, "x"))
like image 391
user1420372 Avatar asked Sep 12 '18 02:09

user1420372


1 Answers

One way could be to convert each row of the list column (col3) to a tibble first and then unnest the result

library(tidyverse)
tmp %>% mutate(new_col = map(col3, as_tibble)) %>% unnest(new_col)

#> # A tibble: 2 x 5
#>   col1  col2  col3       x     b                   
#>   <chr> <chr> <list>     <lgl> <list>              
#> 1 A1    B1    <list [2]> TRUE  <data.frame [3 × 2]>
#> 2 A2    B2    <list [2]> FALSE <data.frame [3 × 2]>
like image 185
markdly Avatar answered Sep 20 '22 03:09

markdly