gpt4 book ai didi

r - 移动均值作为 dplyr 中的函数

转载 作者:行者123 更新时间:2023-12-03 18:32:36 25 4
gpt4 key购买 nike

我想创建一个函数来计算可变数量的最后观测值和不同变量的移动平均值。以此作为模拟数据:

df = expand.grid(site = factor(seq(10)),
year = 2000:2004,
day = 1:50)
df$temp = rpois(dim(df)[1], 5)

计算 1 个变量和固定数量的最后观察值有效。例如。这将计算过去 5 天的平均温度:

library(dplyr)
library(zoo)

df <- df %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(almost_avg = rollmean(x = temp, 5, align = "right", fill = NA)) %>%
mutate(avg = lag(almost_avg, 1))

到目前为止一切顺利。现在尝试功能化失败了。

avg_last_x <- function(dataframe, column, last_x) {

dataframe <- dataframe %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate(almost_avg = rollmean(x = column, k = last_x, align = "right", fill = NA)) %>%
mutate(avg = lag(almost_avg, 1))

return(dataframe) }

avg_last_x(dataframe = df, column = "temp", last_x = 10)

我收到这个错误:

Error in mutate_impl(.data, dots) : k <= n is not TRUE 

我知道这可能与 evaluation mechanism in dplyr 有关,但我没有修好它。

预先感谢您的帮助。

最佳答案

这应该可以解决它。

library(lazyeval)

avg_last_x <- function(dataframe, column, last_x) {
dataframe %>%
group_by(site, year) %>%
arrange(site, year, day) %>%
mutate_(almost_avg = interp(~rollmean(x = c, k = last_x, align = "right",
fill = NA), c = as.name(column)),
avg = ~lag(almost_avg, 1))
}

关于r - 移动均值作为 dplyr 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41520205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com