tidyverse / tidyr

Tidy Messy Data
https://tidyr.tidyverse.org/
Other
1.38k stars 417 forks source link

unnesting list variable with dataframes. #58

Closed adder closed 9 years ago

adder commented 9 years ago

Hi

I recently ran into the situation where I had a dplyr dataframe with one of the variables being a list variable. This list variable contained a dataframe. I wanted to unnest this collumn. Each collumn in the nested dataframes should become a separate collumn in the unnested data frame. As far as I know this is not possible in the current implementation (in straightforward way). What would be the suggested strategy to have this behaviour?

greetz

hadley commented 9 years ago

Could you please provide a reproducible example?

adder commented 9 years ago

off course. I would expect that this code:

library(dplyr)
library(tidyr)
dat1 = data_frame(a = 1:3, b = 4:6)
dat2 = data_frame(c = 7:9,d = list(dat1,dat1,dat1))
unnest(dat2, d)

gives this output:

c a b
7 1 4
7 2 5
7 3 6
8 1 4
8 2 5
8 3 6
9 1 4
9 2 5
9 3 6

You would need to make the assumption that all the dataframes in the list variable are the same. A typical use case could be that on some variables in a dataframe a function is applied rowwise that gives back a dataframe. Then one would want to unnest this for easy data manipulations where also the information in the nested data frames is needed (for grouping, filtering,...)

I hope this is clear enough.

hadley commented 9 years ago

Thanks, that definitely should work. I'll take a look next time I'm working on tidyr