如何按dplyr中的固定行数分组? [英] How to group by a fixed number of rows in dplyr?
本文介绍了如何按dplyr中的固定行数分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框:
set.seed(123)
x <- sample(10)
y <- x^2
my.df <- data.frame(x, y)
结果是这样的:
> 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
我想要的是按每 n 行对行进行分组,以计算均值,总和或5个选定行上的任意值。对于 n = 5 这样的事情:
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))
预期输出将是这样的:
# A tibble: 1 x 2
sum mean
<dbl> <dbl>
1 174 34.8
2 211 42.2
当然,行数数据框中的值可能是15、20、100,无论如何。我仍然希望将数据每 n 行分组。
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.
我该怎么做?
推荐答案
我们可以使用 rep
或 gl
创建分组变量
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
这篇关于如何按dplyr中的固定行数分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文