How to group by a fixed number of rows in dplyr?

We can use rep or gl to create the grouping variable

library(dplyr)
my.df %>% 
    group_by(grp = as.integer(gl(n(), 5, n()))) %>% 
    #or with rep
    # group_by(grp = rep(row_number(), length.out = n(), each = 5)) 
    summarise(sum = sum(y), mean = mean(y))
# A tibble: 2 x 3
#    grp   sum  mean
#  <int> <dbl> <dbl>
#1     1   174  34.8
#2     2   211  42.2

Another option could be:

my.df %>%
 group_by(x = ceiling(row_number()/5)) %>%
 summarise_all(list(sum = sum, mean = mean))

      x   sum  mean
  <dbl> <dbl> <dbl>
1     1   174  34.8
2     2   211  42.2

Tags:

R

Dplyr