Search code examples
rgroup-bydplyr

How to extract one specific group in dplyr


Given a grouped tbl, can I extract one/few groups? Such function can be useful when prototyping code, e.g.:

mtcars %>%
  group_by(cyl) %>%
  select_first_n_groups(2) %>%
  do({'complicated expression'})

Surely, one can do an explicit filter before grouping, but that can be cumbersome.


Solution

  • Try this where groups is a vector of group numbers. Here 1:2 means the first two groups:

    select_groups <- function(data, groups, ...) 
       data[sort(unlist(attr(data, "indices")[ groups ])) + 1, ]
    
    mtcars %>% group_by(cyl) %>% select_groups(1:2)
    

    The selected rows appear in the original order. If you prefer that the rows appear in the order that the groups are specified (e.g. in the above eaxmple the rows of the first group followed by the rows of the second group) then remove the sort.