2

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]>
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Emil11
  • 199
  • 9
  • Do you want `outerNest %>% ungroup %>% mutate(data = map(data, ~ .x %>% nest(data = -am)))` – akrun Oct 08 '22 at 20:51
  • 1
    @akrun that works! I think the difference was `data = -am`, I did not think to do this. – Emil11 Oct 08 '22 at 20:56

1 Answers1

2

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]>
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Ah I see, I had thought that because `.x` was already used, I required a different variable name - silly me! Although, it works when using `.x`. It is obvious there is no clean way around this unless I develop a function to work for `n-1` nests – Emil11 Oct 08 '22 at 21:05
  • You may need a recursive function – akrun Oct 08 '22 at 21:06
  • 1
    That is what I was thinking, I will dig into this function and return here for your kind help if I encounter problems :) – Emil11 Oct 08 '22 at 21:07