7

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?

Ben
  • 6,321
  • 9
  • 40
  • 76
  • 1
    Relevant post: https://stackoverflow.com/questions/3318333/split-a-vector-into-chunks-in-r – zx8754 Mar 18 '19 at 16:50

2 Answers2

12

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
akrun
  • 874,273
  • 37
  • 540
  • 662
4

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
tmfmnk
  • 38,881
  • 4
  • 47
  • 67