在dplyr中查找滑动窗口的方差 [英] Find the variance over a sliding window in dplyr

查看:116
本文介绍了在dplyr中查找滑动窗口的方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查找一组中前三个值的差异。

I want to find the variance of the previous three values in a group.

# make some data with categories a and b
library(dplyr)
df = expand.grid(
  a = LETTERS[1:3],
  index = 1:10
)
# add a variable that changes within each group
set.seed(9999)
df$x = runif(nrow(df))

# get the variance of a subset of x
varSubset = function(x, index, subsetSize) {
  subset = (index-subsetSize+1):index
  ifelse(subset[1]<1, -1, var(x[subset]))
}

df %>%
  # group the data
  group_by(a) %>%
  # get the variance of the 3 most recent values
  mutate(var3 = varSubset(x, index, 3))

它正在调用 varSubset ,同时使用 x 索引作为向量。

It's calling the varSubset with both x and index as vectors.

我无法弄清楚如何将 x 作为向量(仅限于组)和 index 作为单个值。我试过 rowwise(),但是我有效地失去了分组。

I can't figure out how to treat x as a vector (of only the group) and index as a single value. I've tried rowwise(), but then I effectively lose grouping.

推荐答案

为什么不从动物园中使用 rollapply

Why not use rollapply from zoo?:

library(dplyr)

library(zoo)
df %>% group_by(a) %>%
       mutate(var = rollapply(x, 3, var, fill = NA, align = "right"))

这篇关于在dplyr中查找滑动窗口的方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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