R - ave rollapply error: k <= n is not TRUE
Asked Answered
L

2

6

I am trying to calculate a rolling mean of value grouped by multiple dimensions in R. Something I would do in SQL in the following way:

AVG(value) OVER 
   (PARTITION BY dim1, dim2 ORDER BY date 
       RANGE BETWEEN 5 PRECEDING AND CURRENT ROW)

The following seems to work if I select just a few dimensions:

s <- ave(df$value, 
     list(df$dim1, df$dim2), 
     FUN= function(x) rollapply(x, 5, mean, align='right'))

but gives the following error when I select full set of dimensions:

Error: k <= n is not TRUE 

I get the same error when I run:

rollapply(c(1:2), 3, mean, align='right')

so I guess the issue is that some combinations of dimensions do not have enough values to calculate mean.

How could I overcome it? I am fine with having a NA as a result for those combinations. Any help would be much appreciated..

Leuctra answered 3/4, 2014 at 18:16 Comment(2)
Well, in your last example, you are trying to use the last three values on each iteration...but the iteration vector c(1:2) only has two!Dextrad
Yes, I am aware what the issue is, second example is just to illustrate it. The question is, how can I overcome it and get NA if there are to few elements to calculate rollapply instead of getting an error message.Leuctra
G
3

roll_meanr from the RcppRoll package will do this by default:

library(RcppRoll)
> roll_meanr(c(1:2), 3)
# [1] NA NA
Glance answered 22/2, 2016 at 1:26 Comment(0)
A
0

rollapply(c(1:10), 3, mean, align='right', fill=NA) should do the trick, provided your vector is long enough to produce any data.

note that rollapply(c(1:2), 3, mean, align='right', fill=NA) still returns the error for the reason stated by @robert-krzyzanowski

Auer answered 5/5, 2017 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.