[R] "fill in" values between rollapply

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 11 00:04:45 CEST 2010


Here are two  solutions.  Assuming x is a zoo object both return zoo objects:

1. rollapply with na.pad and na.locf:

r <- rollapply(x, 5, mean, by = 5, align = "left", na.pad = TRUE)
na.locf(r)

2. or try this which does not use rollapply at all but assumes x is 1
dimensional:

   n <- length(x)
   a <- ave(x, gl(n, 5, n), FUN = mean)


On Fri, Apr 9, 2010 at 3:32 AM, Brad Patrick Schneid <bpschn01 at gmail.com> wrote:
>
> Hi,
> Sorry ahead of time for not including data with this question.
> Using rollapply to calculate mean values for 5 day blocks, I'd use this:
>
> Roll5mean <- rollapply(data, 5, mean, by=5, align = c("left"))
>
> My question is, can someone tell me how to fill in the days between each of
> these means with the previously calculated mean?  If this doesn't make
> sense, I will clarify and provide data for an example.
>
> Thanks.
> Brad
> --
> View this message in context: http://n4.nabble.com/fill-in-values-between-rollapply-tp1816885p1816885.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list