Quantcast

Duplicate dates in zoo objects

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Duplicate dates in zoo objects

Costas
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Duplicate dates in zoo objects

Joris FA Meys
Try this :
> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14) - 1

> x <- zoo(1:5, x.Date)

> x
2003-02-01 2003-02-03 2003-02-07 2003-02-07 2003-02-14
         1          2          3          4          5

> i <- match(unique(index(x)),rev(index(x)))

> x[i]
2003-02-01 2003-02-03 2003-02-07 2003-02-14
         1          2          4          5

Cheers
Joris


On Tue, Jun 22, 2010 at 4:35 PM, Research <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>



--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Duplicate dates in zoo objects

Achim Zeileis-4
In reply to this post by Costas
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Duplicate dates in zoo objects

Achim Zeileis-4
In reply to this post by Joris FA Meys
On Tue, 22 Jun 2010, Joris Meys wrote:

> Try this :
>> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14) - 1
>
>> x <- zoo(1:5, x.Date)
>
>> x
> 2003-02-01 2003-02-03 2003-02-07 2003-02-07 2003-02-14
>         1          2          3          4          5
>
>> i <- match(unique(index(x)),rev(index(x)))
>
>> x[i]
> 2003-02-01 2003-02-03 2003-02-07 2003-02-14
>         1          2          4          5
Note that this happens to do the right thing in this particular toy
example but not more generally. Simply adding a single observation will
make it fail. The aggregate() solution I posted previously is preferred:

R> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14, 15) - 1
R> x <- zoo(1:6, x.Date)
Warning message:
In zoo(1:6, x.Date) :
   some methods for "zoo" objects do not work if the index entries in
'order.by' are not unique
R> x
2003-02-01 2003-02-03 2003-02-07 2003-02-07 2003-02-14 2003-02-15
          1          2          3          4          5          6
R> aggregate(x, time(x), tail, 1)
2003-02-01 2003-02-03 2003-02-07 2003-02-14 2003-02-15
          1          2          4          5          6
R> i <- match(unique(index(x)),rev(index(x)))
R>  x[i]
2003-02-01 2003-02-03 2003-02-07 2003-02-14 2003-02-15
          1          2          3          5          6

Best,
Z

> Cheers
> Joris
>
>
> On Tue, Jun 22, 2010 at 4:35 PM, Research <[hidden email]> 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
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
>
> --
> Joris Meys
> Statistical consultant
>
> Ghent University
> Faculty of Bioscience Engineering
> Department of Applied mathematics, biometrics and process control
>
> tel : +32 9 264 59 87
> [hidden email]
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>
> ______________________________________________
> [hidden email] 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.
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Duplicate dates in zoo objects

Joris FA Meys
Oops, I was too fast. I meant :

i <- length(x)- match(unique(index(x)),rev(index(x)))+1

(one has to reverse the indices again)

But in any case, the aggregate-way is indeed far cleaner. Thx for
pointing that out.
Cheers
Joris

On Tue, Jun 22, 2010 at 7:24 PM, Achim Zeileis <[hidden email]> wrote:

> On Tue, 22 Jun 2010, Joris Meys wrote:
>
>> Try this :
>>>
>>> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14) - 1
>>
>>> x <- zoo(1:5, x.Date)
>>
>>> x
>>
>> 2003-02-01 2003-02-03 2003-02-07 2003-02-07 2003-02-14
>>        1          2          3          4          5
>>
>>> i <- match(unique(index(x)),rev(index(x)))
>>
>>> x[i]
>>
>> 2003-02-01 2003-02-03 2003-02-07 2003-02-14
>>        1          2          4          5
>
> Note that this happens to do the right thing in this particular toy example
> but not more generally. Simply adding a single observation will make it
> fail. The aggregate() solution I posted previously is preferred:
>
> R> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 7, 14, 15) - 1
> R> x <- zoo(1:6, x.Date)
> Warning message:
> In zoo(1:6, x.Date) :
>  some methods for "zoo" objects do not work if the index entries in
> 'order.by' are not unique
> R> x
> 2003-02-01 2003-02-03 2003-02-07 2003-02-07 2003-02-14 2003-02-15
>         1          2          3          4          5          6
> R> aggregate(x, time(x), tail, 1)
> 2003-02-01 2003-02-03 2003-02-07 2003-02-14 2003-02-15
>         1          2          4          5          6
> R> i <- match(unique(index(x)),rev(index(x)))
> R>  x[i]
> 2003-02-01 2003-02-03 2003-02-07 2003-02-14 2003-02-15
>         1          2          3          5          6
>
> Best,
> Z
>
>> Cheers
>> Joris
>>
>>
>> On Tue, Jun 22, 2010 at 4:35 PM, Research <[hidden email]>
>> 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
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>>
>>
>> --
>> Joris Meys
>> Statistical consultant
>>
>> Ghent University
>> Faculty of Bioscience Engineering
>> Department of Applied mathematics, biometrics and process control
>>
>> tel : +32 9 264 59 87
>> [hidden email]
>> -------------------------------
>> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>>
>> ______________________________________________
>> [hidden email] 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.
>



--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
[hidden email]
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Duplicate dates in zoo objects

Gabor Grothendieck
In reply to this post by Costas
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Loading...