I have a data frame:
set.seed(123)
x <- sample(10)
y <- x^2
my.df <- data.frame(x, y)
The result is this:
> my.
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
#
#1 1 174 34.8
#2 2 211 42.2