如何从下往上计算移动平均值

huangapple go评论98阅读模式
英文:

How to calculate moving average from bottom to up

问题

有一个名为df的数据框,如何添加moving_average变量作为图像?我想要从底部向上计算,只计算两个日期。

library(tidyverse)
df <- data.frame(value=c(1:10)) 

如何从下往上计算移动平均值

英文:

There is dataframe df , how to add moving_average variable as image ? I want to the calculate bottom up and just calculate two dataes.

library(tidyverse)
df &lt;- data.frame(value=c(1:10)) 

如何从下往上计算移动平均值

答案1

得分: 2

这是一个使用zoo包的解决方案。

df <- data.frame(value=0:10) 

df$moving_average <- zoo::rollmeanr(df$value, k = 2L, fill = NA)
is.na(df$moving_average) <- (seq_along(df$value) - 1L) %% 2L == 1L
df
#>    value moving_average
#> 1      0             NA
#> 2      1             NA
#> 3      2            1.5
#> 4      3             NA
#> 5      4            3.5
#> 6      5             NA
#> 7      6            5.5
#> 8      7             NA
#> 9      8            7.5
#> 10     9             NA
#> 11    10            9.5

创建于2023-08-10,使用 reprex v2.0.2

英文:

Here is a solution with package zoo.

df &lt;- data.frame(value=0:10) 

df$moving_average &lt;- zoo::rollmeanr(df$value, k = 2L, fill = NA)
is.na(df$moving_average) &lt;- (seq_along(df$value) - 1L) %% 2L == 1L
df
#&gt;    value moving_average
#&gt; 1      0             NA
#&gt; 2      1             NA
#&gt; 3      2            1.5
#&gt; 4      3             NA
#&gt; 5      4            3.5
#&gt; 6      5             NA
#&gt; 7      6            5.5
#&gt; 8      7             NA
#&gt; 9      8            7.5
#&gt; 10     9             NA
#&gt; 11    10            9.5

<sup>Created on 2023-08-10 with reprex v2.0.2</sup>

答案2

得分: 1

尝试以下类似的ave函数用法:

transform(
    df,
    moving_average = ave(value,
        rev((seq_along(value)-1) %/% 2),
        FUN = \(x) c(NA, mean(x))[seq_along(x)]
    )
)

这会得到以下结果:

   value moving_average
1      1             NA
2      2            1.5
3      3             NA
4      4            3.5
5      5             NA
6      6            5.5
7      7             NA
8      8            7.5
9      9             NA
10    10            9.5
英文:

Try ave like below

transform(
    df,
    moving_average = ave(value,
        rev((seq_along(value)-1) %/% 2),
        FUN = \(x) c(NA, mean(x))[seq_along(x)]
    )
)

which gives

   value moving_average
1      1             NA
2      2            1.5
3      3             NA
4      4            3.5
5      5             NA
6      6            5.5
7      7             NA
8      8            7.5
9      9             NA
10    10            9.5

答案3

得分: 1

使用rollapply函数,参数为by = 2align = "left",然后再将结果反转,或者使用ave函数,将gl反转。

library(zoo)
df <- data.frame(value=0:10) 

transform(df, ma = rev(rollapply(rev(value), 2, mean, by = 2, fill = NA, align = "left")))

# 或者
n <- nrow(df)
transform(df, ma = ave(value, rev(gl(n, 2, n)), FUN = function(x) rollmeanr(x, 2, fill = NA)))

其中任一方法都会得到如下结果:

   value  ma
1      0  NA
2      1  NA
3      2 1.5
4      3  NA
5      4 3.5
6      5  NA
7      6 5.5
8      7  NA
9      8 7.5
10     9  NA
11    10 9.5
英文:

Reverse it, use rollapply with by = 2 and align = &quot;left&quot; and reverse it back or with ave reverse gl.

library(zoo)
df &lt;- data.frame(value=0:10) 

transform(df, ma = rev(rollapply(rev(value), 2, mean, by = 2, fill = NA,
  align = &quot;left&quot;)))

# or
n &lt;- nrow(df)
transform(df, ma = ave(value, rev(gl(n, 2, n)),
  FUN = \(x) rollmeanr(x, 2, fill = NA)))

Either one gives

   value  ma
1      0  NA
2      1  NA
3      2 1.5
4      3  NA
5      4 3.5
6      5  NA
7      6 5.5
8      7  NA
9      8 7.5
10     9  NA
11    10 9.5

huangapple
  • 本文由 发表于 2023年8月10日 15:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定