mr.T
mr.T

Reputation: 634

multiple columns in a sliding window, runner package, xts format

I'm trying to do something similar to w1, which contains colums in a sliding window of size k

set.seed(1)
library(xts)
tm <- Sys.time()+1:5000
x <- rnorm(5000) |> cumsum() |> round(1) |> xts(tm) |> to.minutes5(name = NULL) |> align.time()+100

# window len
k <- 3
w1 <- as.zoo(x) |> rollapplyr(width=k, FUN=c, fill=NA)

I'm trying to get the same thing as w1 using the runner package but I'm doing something wrong. This solution does not work although it is indicated that the package works with the xts format

library(runner)
w2 <- runner(x, k=k, f=c, na_pad = T) 

I made a workaround to make it work, but I would like to know how to do it correctly and non-redundantly.

w2 <- x |> 
      coredata() |> 
      runner(k=k, f=c, na_pad = T) |> 
      do.call(what="rbind") |> 
      xts(index(x))

Upvotes: 0

Views: 17

Answers (0)

Related Questions