Home > OS >  Nest dataframe within a nested dataframe
Nest dataframe within a nested dataframe

Time:10-09

I have searched this solution: Double nesting in the tidyverse however, this seems to mutate an additional column for the outer nest. Whereas, I wanted to nest within the nested list, and keep nesting.

By following the solution in that referenced article I get:

> outerNest %>% mutate(by_vs = map(data, ~.x %>% 
                                              group_by(am) %>% 
                                              nest()))
# A tibble: 2 × 3
# Groups:   vs [2]
     vs data               by_vs               
  <dbl> <list>             <list>              
1     0 <tibble [18 × 11]> <grouped_df [2 × 2]>
2     1 <tibble [14 × 11]> <grouped_df [2 × 2]>

Where I have tried the following:

outerNest %>% group_by(vs) %>% mutate_at(vars(matches("data")), map(.,function(x){map(x, function(y){ y %>% group_by(am) %>% nest()})}))

Which produces this error:

Error in UseMethod("group_by") : no applicable method for 'group_by' applied to an object of class "c('double', 'numeric')"

There seems to be an issue with select data as a column to iterate the nesting in. Whereas, If I select the column individually:

outerNest %>% group_by(vs) %>% .['data'] %>% map(.,function(x){map(x, function(y){ y %>% group_by(am) %>% nest()})})

$data
$data[[1]]
# A tibble: 2 × 2
# Groups:   am [2]
     am data              
  <dbl> <list>            
1     1 <tibble [6 × 10]> 
2     0 <tibble [12 × 10]>

$data[[2]]
# A tibble: 2 × 2
# Groups:   am [2]
     am data             
  <dbl> <list>           
1     1 <tibble [7 × 10]>
2     0 <tibble [7 × 10]>

However this should be nested within data next to vs`. The expected output:

#most outer nest
# A tibble: 2 × 2
# Groups:   vs [2]
     vs data              
  <dbl> <list>            
1     0 <tibble [18 × 11]>
2     1 <tibble [14 × 11]>

#inner nest

# A tibble: 2 × 2
# Groups:   am [2]
     am data             
  <dbl> <list>           
1     1 <tibble [13 × 20]>
2     0 <tibble [19 × 20]>

CodePudding user response:

We could use

library(purrr)
library(dplyr)
outerNest %>%
    ungroup %>%
    mutate(data = map(data, ~ .x %>% 
    nest(data = -am)))

If it needs to be nested further

out <- outerNest %>% 
  ungroup %>%
    mutate(data = map(data, ~ .x %>%
       nest(data = -am) %>%
            mutate(data = map(data, ~ .x %>%
          nest(data = -gear))))) 

-checking

> out$data[[1]]
# A tibble: 2 × 2
     am data            
  <dbl> <list>          
1     1 <tibble [2 × 2]>
2     0 <tibble [1 × 2]>

> out$data[[1]]$data
[[1]]
# A tibble: 2 × 2
   gear data            
  <dbl> <list>          
1     4 <tibble [2 × 9]>
2     5 <tibble [4 × 9]>
  • Related