rollmean with dplyr and magrittr

扶醉桌前 提交于 2019-11-28 09:18:59

May be this helps:

library(dplyr)
data %>%
group_by(o) %>%
mutate(rM=rollmean(u,3, na.pad=TRUE, align="right"))

If you want to do for both columns, u and v

fun1 <- function(x) rollmean(x, 3, na.pad=TRUE, align="right")
data %>% 
group_by(o) %>% 
mutate_each(funs(fun1), u, v)

A more flexible wrapper comes from the rowr package. This allows for windows of different size within your initial data.

data %>% 
group_by(o) %>% 
mutate(MEANS = rollApply(u, fun=mean, window=3, align='right'))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!