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"))
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]>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With