[Solved] Using group_by() in dplyr for certain number of rows


You just want to group on-the-fly, which means passing an ad hoc variable to the grouper.

In data.table, this would be done by using rep in by:

library(data.table)
set.seed(01394)
DT <- data.table(VAR = rnorm(2520))

DT[ , mean(VAR), by = rep(1:18, each = 140)]
#     rep           V1
#  1:   1  0.029683200
#  2:   2  0.150411987
#  3:   3  0.061912697
#  4:   4 -0.014229183
#  5:   5 -0.007305455
#  6:   6  0.003784550
#  7:   7  0.010941501
#  8:   8  0.032129151
#  9:   9 -0.036524921
# 10:  10  0.046986451
# 11:  11  0.075547228
# 12:  12 -0.079538420
# 13:  13 -0.099833001
# 14:  14 -0.065297462
# 15:  15 -0.020778063
# 16:  16  0.095983764
# 17:  17  0.095292230
# 18:  18  0.150405148

As per @docendodecimus, the dplyr equivalent is:

DT %>% group_by(g = rep(1:18, each = 140)) %>% summarise(m = mean(VAR))

0

solved Using group_by() in dplyr for certain number of rows