Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include the grouping column in nested data

Tags:

r

purrr

I want to run some modelling on each group of variables a and b. The problem is that nest() doesn't include the grouping variables which are needed by the model.

expand.grid(a = LETTERS[1:3], b = LETTERS[1:2], c=1:3, d=1:3) %>% 
   group_by(a, b) %>% 
   nest()

The resulting table includes a and b on the "outside" and c and d in the nested tibble. How can I add a and b to the nested tibble?

like image 397
sharoz Avatar asked Sep 18 '25 03:09

sharoz


1 Answers

Using cur_data_all() this creates a 3 column data frame in which the last column, nest, is a list each of whose components is the 4 column data frame in one a,b group.

ans <- expand.grid(a = LETTERS[1:3], b = LETTERS[1:2], c=1:3, d=1:3) %>% 
   group_by(a, b) %>% 
   summarize(nest = list(cur_data_all()), .groups = "drop")

giving:

> ans
# A tibble: 6 x 3
  a     b     nest            
  <fct> <fct> <list>          
1 A     A     <tibble [9 x 4]>
2 A     B     <tibble [9 x 4]>
3 B     A     <tibble [9 x 4]>
4 B     B     <tibble [9 x 4]>
5 C     A     <tibble [9 x 4]>
6 C     B     <tibble [9 x 4]>

> names(ans$nest[[1]])
[1] "a" "b" "c" "d"

If a data frame with just a single ccolumn, nest, is desired equal to the nest column above (except for attributes) then this code would work.

expand.grid(a = LETTERS[1:3], b = LETTERS[1:2], c=1:3, d=1:3) %>% 
  group_modify(~ tibble(nest = group_split(., a, b)))
like image 156
G. Grothendieck Avatar answered Sep 20 '25 19:09

G. Grothendieck