[英]how to calculate “consecutive mean” in R without using loop, or in a more efficient way?
I have a set a data that I need to calculate their "consecutive mean" (I dunno if it is the correct name, but I can't find anything better), here is an example: 我设置了一个数据,我需要这些数据来计算它们的“连续平均值”(如果它是正确的名称,我不知道,但是我找不到更好的东西了),下面是一个示例:
ID Var2 Var3
1 A 1
2 A 3
3 A 5
4 A 7
5 A 9
6 A 11
7 B 2
8 B 4
9 B 6
10 B 8
11 B 10
Here I need to calculated the mean of 3 Var3 variable in the same subset consecutively (ie there will be 4 means caulculated for A: mean(1,3,5), mean(3,5,7), mean(5,7,9), mean(7,9,11), and 3 means calculated for B: mean(2,4,6), mean(4,6,8), mean(6,8,10). And the result should be: 在这里,我需要连续计算同一子集中的3个Var3变量的均值(即,将为A取4个均值:均值(1、3、5),均值(3、5、7),均值(5、7) ,9),mean(7,9,11)和为B计算的3个均值:mean(2,4,6),mean(4,6,8),mean(6,8,10)。应该:
ID Var2 Var3 Mean
1 A 1 N/A
2 A 3 N/A
3 A 5 3
4 A 7 5
5 A 9 7
6 A 11 9
7 B 2 N/A
8 B 4 N/A
9 B 6 4
10 B 8 6
11 B 10 8
Currently I am using a "loop-inside-a-loop" approach, I subset the dataset using Var2, and then I calculated the mean in another start from the third data. 目前,我正在使用“内部循环”方法,使用Var2对数据集进行子集设置,然后从第三数据开始的另一个起点中计算平均值。
It suits what I need, but it is very slow, is there any faster way for this problem? 它适合我的需求,但是速度很慢,有没有解决这个问题的更快方法?
Thanks! 谢谢!
It's generally referred to as a "rolling mean" or "running mean". 通常称为“滚动平均值”或“运行平均值”。 The
plyr
package allows you to calculate a function over segments of your data and the zoo
package has methods for rolling calculations. plyr
软件包使您可以对数据段进行计算,而zoo
软件包具有滚动计算方法。
> lines <- "ID,Var2,Var3
+ 1,A,1
+ 2,A,3
+ 3,A,5
+ 4,A,7
+ 5,A,9
+ 6,A,11
+ 7,B,2
+ 8,B,4
+ 9,B,6
+ 10,B,8
+ 11,B,10"
>
> x <- read.csv(con <- textConnection(lines))
> close(con)
>
> ddply(x,"Var2",function(y) data.frame(y,
+ mean=rollmean(y$Var3,3,na.pad=TRUE,align="right")))
ID Var2 Var3 mean
1 1 A 1 NA
2 2 A 3 NA
3 3 A 5 3
4 4 A 7 5
5 5 A 9 7
6 6 A 11 9
7 7 B 2 NA
8 8 B 4 NA
9 9 B 6 4
10 10 B 8 6
11 11 B 10 8
交替使用基数R
x$mean <- unlist(tapply(x$Var3, x$Var2, zoo::rollmean, k=3, na.pad=TRUE, align="right", simplity=FALSE))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.