[R] differecing a zoo series

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 20 20:24:59 CEST 2010


On Fri, Aug 20, 2010 at 2:19 PM, steven mosher <moshersteven at gmail.com> wrote:
> A quick question
>
>  x <- as.yearmon(2000 + seq(0, 23)/12)
>  x
>  [1] "Jan 2000" "Feb 2000" "Mar 2000" "Apr 2000" "May 2000" "Jun 2000" "Jul
> 2000" "Aug 2000" "Sep 2000" "Oct 2000" "Nov 2000" "Dec 2000" "Jan 2001"
> [14] "Feb 2001" "Mar 2001" "Apr 2001" "May 2001" "Jun 2001" "Jul 2001" "Aug
> 2001" "Sep 2001" "Oct 2001" "Nov 2001" "Dec 2001"
>  data<-seq(1,24,by=1)
>  testzoo<-zoo(data,order.by=x)
>
> The operation I ant to perform on the zoo series is this. I will illustrate
> with a small example and formula:
>
> the coredata of the zoo series is  1,2,3,4,5,6,7,8    )
> I want to calculate Result<- zoo[x]-zoo[x-1]  (NA,1,1,1,1,1...NA)
> The first element of course is undefined(NA). is there any method to do this
> elegantly, padding NAs at the
> start  works but its ugly. if I get a simple function I can apply it to a
> matrix of zoo series

zoo has a diff.zoo method:

   diff(testzoo)
   diff(testzoo, na.pad = TRUE)



More information about the R-help mailing list