1

This seems to me a very simple question but I don't manage to come up with an efficient idea.

I have a data frame in R so composed:

  • column position generated as seq(from = 1, to = nrow(df), by = 1)
  • column value, with some values associated with the position

I want to group the dataframe each k rows (k being an integer input) and then calculate the mean of each group.

The dplyr function group_by does not allow me to group for a specific integer number of rows.

How can I do that? Is there a way to avoid creating the column position at all?

Vitomir
  • 295
  • 2
  • 14

1 Answers1

1

Here is one option with gl from base R. Specify the n and k values. The n would be the total number of rows in the dataset

library(dplyr)
k1 <- 5
df1 %>% 
  group_by(grp = as.integer(gl(n(), k = k1, n()))) %>% 
  summarise(value = mean(value))
akrun
  • 874,273
  • 37
  • 540
  • 662