在 data.table 行上滚动应用函数中的子集计算

Rollapply over data.table rows with subset calculations in function

我想在 data.table 上滚动应用一个函数。在函数中,我想使用 data.table 子集,以便下面的示例有效。

library(zoo)
library(data.table)

dt <- data.table(i = 1:100,
                       x = sample(1:10, 100, replace = T),
                       y = sample(1:10, 100, replace = T))

rollapply(dt, width=10, FUN = function(dt_slice) dt_slice[, mean(x == y)])

您可以使用 rollapplysapply/outer 来获取索引矩阵,然后 apply 在该矩阵上使用您想要的操作

inds <- rollapply(seq_len(nrow(dt)), width = 10, FUN = I)
# or inds <- t(sapply(seq_len(1 + nrow(dt) - 10) - 1, `+`, 1:10))
# or inds <- outer(seq_len(1 + nrow(dt) - 10) - 1, 1:10, `+`)
# or inds <- embed(1:100, 10)[, 10:1] # thanks @Frank
apply(inds, 1, function(i) dt[i, mean(x == y)])

#  [1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
# [20] 0.0 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.2 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1
# [39] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.1 0.1
# [58] 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.0 0.0 0.0 0.0
# [77] 0.1 0.1 0.1 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.1 0.1 0.1 0.0 0.0

虽然像这个例子一样简单的操作你也可以

dt[, rollapply(x == y, width = 10, FUN = mean)]

感谢@jangorecki 提及 frollapply 函数。这是添加到 data.table 库中的另一件美事。对于你的问题,你会 运行 以下内容:

library(data.table)
set.seed(17)
dt <- data.table(i = 1:100,
             x = sample(1:10, 100, replace = T),
             y = sample(1:10, 100, replace = T))
dt$index <- dt$x == dt$y
dt[,`:=` (MA = frollapply(index,10,mean)), ]
head(dt,12)