[R] Duplicate dates in zoo objects

Achim Zeileis Achim.Zeileis at uibk.ac.at
Tue Jun 22 18:01:46 CEST 2010


On Tue, 22 Jun 2010, Research wrote:

> Hello,
>
> I have a zoo time series read from an excel file which has some dates the 
> same, such as the following example:
>
> 02/10/1995     4925.5
> 30/10/1995     4915.9
> 23/01/1996     4963.5
> 23/01/1996     5009.2
> 04/03/1996     5031.9     # here
> 04/03/1996     5006.5     # here
> 03/04/1996     5069.2
> 03/05/1996     5103.7
> 31/05/1996     5107.1
> 01/07/1996     5153.1
> 02/08/1996     5151.7
>
> Is there a simple way to keep the last  price of the ones that have the same 
> dates?
>
> 04/03/1996    5031.9
> 04/03/1996    5006.5
>
> i.e., keep only the "04/03/1996    5006.5"  price and discard the previous 
> one... Is there an implicit function that does that or do I need some sort of 
> recursive algorithm?

No, it's very simple and covered as the #1 item in the zoo FAQ:
   vignette("zoo-faq", package = "zoo")

> You can try a solution on this example (for convenience):
>
> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14) - 1
> x <- zoo(rnorm(5), x.Date)

You can use aggregate() to remove the duplicates: For example
   aggregate(x, time(x), mean)
would use the mean price for each Date.

If you want to compute the last observation, the function tail(..., 1) can 
be utilized:
   aggregate(x, time(x), tail, 1)

Best,
Z

> Zoo object  has 2 prices with same dates.
>
> Many thanks in advance,
> Costas
>
> ______________________________________________
> 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