Search code examples
rdplyrtidyr

Unnest only one column of nested data frames


Given a nested data frame like for example

library(tidyr)
library(dplyr)
m <- mtcars %>% nest_by(cyl)

I want to extract one of the columns in the nested data frames to the top level, creating more rows but not as many as if I would use m %>% unnest(data)

What works for me and gives me the output I want is

m %>% rowwise(-data) %>% summarise(nest_by(data, gear), .groups = "drop")

However, I feel as if I chose a convoluted road to my target because what I want to achieve is partial un-nesting, and I do it by first making an additional nesting that is then unnested again using the summary(<tibble>) semantics.

I tried m %>% hoist(data, "cyl") but this only got me an additional list column.

Is there a canonical function in dplyr that conveys the intention "partial unnesting" better than my solution?


Solution

  • You don't need rowwise, because nest_by() already gives a rowwise grouped tibble. Since dplyr 1.1.0, summarise should be replaced with reframe which is more appropriate for those situations where multiple rows are returned by group:

    m %>% reframe(nest_by(data, gear))
    
    # # A tibble: 8 × 3
    #     cyl  gear               data
    #   <dbl> <dbl> <list<tibble[,9]>>
    # 1     4     3            [1 × 9]
    # 2     4     4            [8 × 9]
    # 3     4     5            [2 × 9]
    # 4     6     3            [2 × 9]
    # 5     6     4            [4 × 9]
    # 6     6     5            [1 × 9]
    # 7     8     3           [12 × 9]
    # 8     8     5            [2 × 9]