How to get the date of specific value within a zoo object?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the date of specific value within a zoo object?

Richard Ma
This post was updated on .
Hi all,

I have a zoo time series object, see below code:
-------------------------------------------------------------------------------------------

# This is a pasteable example
library(zoo)

x <- c(1, 2, 3, 4, 5)
dt <- c("2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04", "2011-01-05")
ts <- zoo(x, dt)

-------------------------------------------------------------------------------------------

Now, I want to know the date when 'ts' equal to a specific value. In this example, suppose I want to know when the ts = 3? The program should return "2011-01-03".

Is there a function to do this job conveniently?

Regards,
Richard
Richard Ma
PhD student, Ecology & Remote Sensing
Climate Change Cluster, Department of Environment Science
University of Technology, Sydney
http://everydropr.wordpress.com
Reply | Threaded
Open this post in threaded view
|

Re: How to get the date of specific value within a zoo object?

Michael Weylandt
I'd suggest you look into the xts class and write

require(xts)
xts = as.xts(1:5,Sys.Date()+1:5)
time(xts)[xts==3]

By the way, your code isn't pastable for me: not sure why.

Michael Weylandt

On Tue, Aug 9, 2011 at 10:39 PM, Richard Ma <[hidden email]> wrote:

> Hi all,
>
> I have a zoo time series object, see below code:
>
> -------------------------------------------------------------------------------------------
>
> # This is a pasteable example
> library(zoo)
>
> x <- c(1, 2, 3, 4, 5)
> dt <- c("2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04",
> "201-01-05")
> ts <- zoo(x, ts)
>
>
> -------------------------------------------------------------------------------------------
>
> Now, I want to know the date when 'ts' equal to a specific value. In this
> example, suppose I want to know when the ts = 3? The program should return
> "2011-01-03".
>
> Is there a function to do this job conveniently?
>
> Regards,
> Richard
>
> -----
> Richard Ma
> PhD student, Ecology & Remote Sensing
> Climate Change Cluster, Department of Environment Science
> University of Technology, Sydney
> http://everydropr.wordpress.com
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-to-get-the-date-of-specific-value-within-a-zoo-object-tp3731885p3731885.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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
|

Re: How to get the date of specific value within a zoo object?

Michael Weylandt
D'Oh -- just spotted it (don't know what was wrong with me):

in your code: "2011-01-05" and ts = zoo(x dt) need to be changed.

If you do that, the same trick

time(ts)[ts==3]

will work. Still, I'm a big xts fan.

Michael

On Tue, Aug 9, 2011 at 10:45 PM, R. Michael Weylandt <
[hidden email]> wrote:

> I'd suggest you look into the xts class and write
>
> require(xts)
> xts = as.xts(1:5,Sys.Date()+1:5)
> time(xts)[xts==3]
>
> By the way, your code isn't pastable for me: not sure why.
>
> Michael Weylandt
>
>
> On Tue, Aug 9, 2011 at 10:39 PM, Richard Ma <[hidden email]>wrote:
>
>> Hi all,
>>
>> I have a zoo time series object, see below code:
>>
>> -------------------------------------------------------------------------------------------
>>
>> # This is a pasteable example
>> library(zoo)
>>
>> x <- c(1, 2, 3, 4, 5)
>> dt <- c("2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04",
>> "201-01-05")
>> ts <- zoo(x, ts)
>>
>>
>> -------------------------------------------------------------------------------------------
>>
>> Now, I want to know the date when 'ts' equal to a specific value. In this
>> example, suppose I want to know when the ts = 3? The program should return
>> "2011-01-03".
>>
>> Is there a function to do this job conveniently?
>>
>> Regards,
>> Richard
>>
>> -----
>> Richard Ma
>> PhD student, Ecology & Remote Sensing
>> Climate Change Cluster, Department of Environment Science
>> University of Technology, Sydney
>> http://everydropr.wordpress.com
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/How-to-get-the-date-of-specific-value-within-a-zoo-object-tp3731885p3731885.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[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
|

Re: How to get the date of specific value within a zoo object?

Richard Ma
In reply to this post by Michael Weylandt
Hi Michael,

Thanks for your kindly help. Problem solved!

Just curious why you prefer "xts" rather than "zoo"? Is "xts" more powerful?

BTW,  It's my mistake that incorrectly type the code. ;-)

Cheers,
Richard

R. Michael Weylandt <michael.weylandt@gmail.com> wrote
I'd suggest you look into the xts class and write

require(xts)
xts = as.xts(1:5,Sys.Date()+1:5)
time(xts)[xts==3]

By the way, your code isn't pastable for me: not sure why.

Michael Weylandt
Richard Ma
PhD student, Ecology & Remote Sensing
Climate Change Cluster, Department of Environment Science
University of Technology, Sydney
http://everydropr.wordpress.com
Reply | Threaded
Open this post in threaded view
|

Re: How to get the date of specific value within a zoo object?

Michael Weylandt
Xts is an extension of zoo that has some other nice features: character subsetting, periodic apply functions, good built in time conversions, etc.

More importantly for my work, the authors put a lot of work into making sure it plays well with all of R's many ts classes so I almost always start any project by changing input from any source to xts so I don't have to think about the occasional inconsistencies.

If you arent working with multiple data sources and don't need the extended functionality, there's absolutely nothing wrong with zoo - it's also a great package.

Michael Weylandt

PS - more than anything, I was somewhat distracted when answering and couldn't get your code to work so I falsely assumed it was a zoo problem: so I just wrote an example I knew worked in xts - when I thought for a moment and saw your bug, I realized the same trick would work in zoo.

On Aug 10, 2011, at 1:53 AM, Richard Ma <[hidden email]> wrote:

> Hi Michael,
>
> Thanks for your kindly help. Problem solved!
>
> Just curious why you prefer "xts" rather than "zoo"? Is "xts" more powerful?
>
> BTW,  It's my mistake that incorrectly type the code. ;-)
>
> Cheers,
> Richard
>
>
> R. Michael Weylandt &lt;[hidden email]&gt; wrote:
>>
>> I'd suggest you look into the xts class and write
>>
>> require(xts)
>> xts = as.xts(1:5,Sys.Date()+1:5)
>> time(xts)[xts==3]
>>
>> By the way, your code isn't pastable for me: not sure why.
>>
>> Michael Weylandt
>>
>
>
> -----
> Richard Ma
> PhD student, Ecology & Remote Sensing
> Climate Change Cluster, Department of Environment Science
> University of Technology, Sydney
> http://everydropr.wordpress.com
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-get-the-date-of-specific-value-within-a-zoo-object-tp3731885p3732108.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.