简体   繁体   English

如何对每个 integer 行数的 data.frame 进行分组?

[英]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:我在 R 中有一个数据框,因此组成:

  • column position generated as seq(from = 1, to = nrow(df), by = 1)position生成为seq(from = 1, to = nrow(df), by = 1)
  • column value , with some values associated with the positionvalue ,其中一些值与 position 关联

I want to group the dataframe each k rows (k being an integer input) and then calculate the mean of each group.我想将 dataframe 每 k 行(k 是 integer 输入)分组,然后计算每组的平均值。

The dplyr function group_by does not allow me to group for a specific integer number of rows. dplyr function group_by不允许我针对特定的 integer 行数进行分组。

How can I do that?我怎样才能做到这一点? Is there a way to avoid creating the column position at all?有没有办法完全避免创建 position 列?

Here is one option with gl from base R .这是来自base Rgl的一个选项。 Specify the n and k values.指定nk值。 The n would be the total number of rows in the dataset n将是数据集中的总行数

library(dplyr)
k1 <- 5
df1 %>% 
  group_by(grp = as.integer(gl(n(), k = k1, n()))) %>% 
  summarise(value = mean(value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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