查找4个最后元素的平均值 [英] Find average of 4 last elements

查看:155
本文介绍了查找4个最后元素的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集具有以下形式:

My dataset has the following form:

df<- data.frame(c("a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b"),
                c(1,    1,   1,   1,   2,   2,   2,   2,   1,   1,    1,  1,   2,    2,   2,   2),
                c(1,    2,   3,   4,   1,   2,   3,   4,   1,   2,   3 , 4,  1,    2,   3,   4),
                c(25,   75,  20,  40,  60,  50,  20,  10,  20,  30,  40,  60, 25,   75,  20,  40))
colnames(df)<-c("car", "year", "mnth", "val")

为了清楚起见,我也在这里显示:

For clarity I show it here as well:

   car year mnth val
1    a    1    1  25
2    a    1    2  75
3    a    1    3  20
4    a    1    4  40
5    a    2    1  60
6    a    2    2  50
7    a    2    3  20
8    a    2    4  10
9    b    1    1  20
10   b    1    2  30
11   b    1    3  40
12   b    1    4  60
13   b    2    1  25
14   b    2    2  75
15   b    2    3  20
16   b    2    4  40

我想在 tmp 添加一个新列 df 其中,对于特定行, tmp 应该是 df $ val 和前三个值的平均值。 tmp 的一些示例显示在此

I would like to add a new column tmp to df where, for a particular row, the value of tmp should be the average of df$val and the 3 preceeding values. Some examples of tmp are shown here

#row 3: mean(25,75,20)=40
#row 4: mean(25,75,20,40)=40
#row 5: mean(75,20,40,60)=48.75
#row 16: mean(25,75,20,40)=40

有没有 -loops?

Is there an efficient way to do this in R without using for-loops?

推荐答案

>对于每个值,计算滚动窗口的平均值,其中包括该值以及前3个值(从索引 i-3 直到索引在下面的解决方案)。对于 i-3 为负的情况,您只需使用 0 max(( i-3),0)

For each value, calculate the mean of a rolling window which includes the value as well as preceding 3 values (from index i-3 up to index i in the solution below). For cases when i-3 is negative, you can just use 0 (max((i-3),0))

sapply(seq_along(df$val), function(i)
      mean(df$val[max((i-3),0):i], na.rm = TRUE))
#[1] 25.00 50.00 40.00 40.00 48.75 42.50 42.50 35.00 25.00
#[10] 20.00 25.00 37.50 38.75 50.00 45.00 40.00

还要考虑 rollmean 动物园

library(zoo)
c(rep(NA,3), rollmean(x = df$val, k = 4))
#[1]    NA    NA    NA 40.00 48.75 42.50 42.50 35.00 25.00 20.00 25.00
#[12] 37.50 38.75 50.00 45.00 40.00
#FURTHER TWEAKING MAY BE NECESSARY

这篇关于查找4个最后元素的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆