Re: [R] Duplicate dates in zoo objects

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 22 Jun 2010 14:56:25 -0400

See the aggregate= argument to read.zoo which will aggregate them as it reads them in like this:

> library(zoo)
> Lines <- "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"

>
> tail1 <- function(x) tail(x, 1)
> z <- read.zoo(textConnection(Lines), format = "%d/%m/%Y", aggregate = tail1)
> z

1995-10-02 1995-10-30 1996-01-23 1996-03-04 1996-04-03 1996-05-03 1996-05-31

    4925.5 4915.9 5009.2 5006.5 5069.2 5103.7 5107.1 1996-07-01 1996-08-02

    5153.1 5151.7

On Tue, Jun 22, 2010 at 10:35 AM, Research <risk2009_at_ath.forthnet.gr> 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?
>
> 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)
>
> 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.
>



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. Received on Tue 22 Jun 2010 - 18:59:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jun 2010 - 19:00:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive