R data.table sliding window

后端 未结 4 1118
谎友^
谎友^ 2020-12-02 15:40

What is the best (fastest) way to implement a sliding window function with the data.table package?

I\'m trying to calculate a rolling median but have multiple rows p

4条回答
  •  情深已故
    2020-12-02 15:59

    I address this in a related thread: https://stackoverflow.com/a/62399700/7115566

    I suggest looking into the frollapply function. For instance, see below

    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)
    

提交回复
热议问题