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.df
x y
1 3 9
2 8 64
3 4 16
4 7 49
5 6 36
6 1 1
7 10 100
8 9 81
9 2 4
10 5 25
What I want is to group the rows by every n rows to compute the mean, sum, or whatever on the 5 selected rows. Something like this for n=5:
my.df %>% group_by(5) %>% summarise(sum = sum(y), mean = mean(y))
The expected output would be something like:
# A tibble: 1 x 2
sum mean
<dbl> <dbl>
1 174 34.8
2 211 42.2
Of course, the number of rows in the data frame could be 15, 20, 100, whatever. I still want to group the data every n rows.
How can I do this?