简体   繁体   中英

How to group a data.frame each integer number of rows?

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM