[R] Indexing zoo objects

Achim Zeileis Achim.Zeileis at uibk.ac.at
Wed Mar 3 10:00:35 CET 2010


On Wed, 3 Mar 2010, Worik R wrote:

> I have a zoo object z
>
> z
>           Value
> 2003-11-15  2.22
> 2003-11-17  2.26
> 2003-11-19  2.28
> 2003-11-22  2.54
> 2003-11-26  2.55
>
> I wish to find the entry 2 entries before "2003-11-26".  How do I do this?

If you want to find out that this is at the third position you can ask

   which(time(z) == "2003-11-26") - 2

and the whole observation could be extracted via

   z[which(time(z) == "2003-11-26") - 2, ]

If you want to query just the data value you could also use lag(), e.g.

   window(lag(z, -2), as.Date("2003-11-26"))

hth,
Z

> I thought I might be able to say index(z["2003-11-26"]) and have it return 5
> so I could then say z[3].  But this does not work.
>
> I can not find an answer in ?index
>
> In the meantime I am using a loop, can any one help?
>
> cheers
> Worik
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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