简体   繁体   English

在 data.table 的两列上滚动 function

[英]Rolling a function on two columns in data.table

I have a data.table as follows -我有一个 data.table 如下 -

library(data.table)
dt = data.table(
  date = seq(as.Date("2015-12-01"), as.Date("2015-12-10"), by="days"),
  v1 = seq(1, 10),
  v2 = c(5, rep(NA, 9))
)
dt
          date v1 v2
 1: 2015-12-01  1  5
 2: 2015-12-02  2 NA
 3: 2015-12-03  3 NA
 4: 2015-12-04  4 NA
 5: 2015-12-05  5 NA
 6: 2015-12-06  6 NA
 7: 2015-12-07  7 NA
 8: 2015-12-08  8 NA
 9: 2015-12-09  9 NA
10: 2015-12-10 10 NA

I want to roll apply the function qma to the current row value of v1 and the previous row value of v2 qma <- function(x, y){(x+y+7)/2}我想将 function qma 滚动应用到 v1 的当前行值和 v2 qma <- function(x, y){(x+y+7)/2}的前一行值

I am sure there must be a simple way to do this in one line using zoo::rollapplyr or data.table.我确信必须有一种简单的方法可以使用 zoo::rollapplyr 或 data.table 在一行中执行此操作。

This is a follow-up question of the original one here R - Rolling sum of two columns in data.table这是原始问题的后续问题R - data.table 中两列的滚动和

For such recursive calculation you may use Reduce here:对于这样的递归计算,您可以在此处使用Reduce

library(data.table)

dt[, v2 := Reduce(qma, v1[-1], init = first(v2), accumulate = TRUE)]
dt

#          date v1       v2
# 1: 2015-12-01  1  5.00000
# 2: 2015-12-02  2  7.00000
# 3: 2015-12-03  3  8.50000
# 4: 2015-12-04  4  9.75000
# 5: 2015-12-05  5 10.87500
# 6: 2015-12-06  6 11.93750
# 7: 2015-12-07  7 12.96875
# 8: 2015-12-08  8 13.98438
# 9: 2015-12-09  9 14.99219
#10: 2015-12-10 10 15.99609

Reduce when used with accumulate = TRUE performs recursive calculation output of which is dependent on previous output.accumulate = TRUE一起使用时, Reduce执行递归计算 output,其依赖于先前的 output。

Take a simple example of calculating cumulative sum.举一个计算累积和的简单例子。

x <- 1:10
res <- Reduce(`+`, x, accumulate = TRUE)
res
#[1]  1  3  6 10 15 21 28 36 45 55

res[1] is x[1] , res[2] is res[1] + x[2] , res[3] is res[2] + x[3] and so on. res[1]x[1]res[2]res[1] + x[2]res[3]res[2] + x[3]等等。

We can use accumulate from purrr我们可以从purrr中使用accumulate

library(dplyr)
library(purrr)
dt %>%
    mutate(v2 = accumulate(v1[-1], qma, .init = first(v2)))
#          date v1       v2
# 1: 2015-12-01  1  5.00000
# 2: 2015-12-02  2  7.00000
# 3: 2015-12-03  3  8.50000
# 4: 2015-12-04  4  9.75000
# 5: 2015-12-05  5 10.87500
# 6: 2015-12-06  6 11.93750
# 7: 2015-12-07  7 12.96875
# 8: 2015-12-08  8 13.98438
# 9: 2015-12-09  9 14.99219
#10: 2015-12-10 10 15.99609

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM