Re: [R] working with zoo time index ??

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 16 Jun 2010 15:41:13 -0400

On Wed, Jun 16, 2010 at 12:40 PM, skan <juanpide_at_gmail.com> wrote:
>
> Hi
> I'll ask in a different way...
>
> I have all this in a file.txt
>
> 1990-01-01 10:00:00 ,  0.900          #  element 1
> 1990-01-01 10:01:00 ,  0.910          #  element 2
> 1990-01-01 10:03:00 ,  0.905          #  element 3
> 1990-01-01 10:04:00 ,  0.905          #  element 4
> 1990-01-01 10:05:00 ,  0.890          #  element 5
> ..................................................................
> 2000-12-30 20:00:00 ,  11.233        # element 3323232
>
> How do I loop through the index? first element 1, then element 2, then the
> third...

That is not the R way. Why do you want to loop through it? What do you want to do? The R way is the whole object approach.

> How do I extract the day or the hour or the minutes from an element from the
> index(element n)?

See R News 4/1 for info on dates and times. See the three vignettes in zoo and the help files for info on zoo. There are tons of examples there.

Here is some code. The loops are not recommended but are only there since you asked.

Lines <- "1990-01-01 10:00:00 ,  0.900          #  element 1
1990-01-01 10:01:00 ,  0.910          #  element 2
1990-01-01 10:03:00 ,  0.905          #  element 3
1990-01-01 10:04:00 ,  0.905          #  element 4
1990-01-01 10:05:00 ,  0.890          #  element 5
2000-12-30 20:00:00 ,  11.233        # element 3323232"

library(zoo)
library(chron)
z <- read.zoo(textConnection(Lines), sep = ",", FUN = as.chron) z

for(i in seq_along(z)) print(time(z)[i]) for(i in seq_along(z)) print(coredata(z)[i])

hours(time(z))
minutes(time(z))



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 Wed 16 Jun 2010 - 19:48:54 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 Wed 16 Jun 2010 - 20:00:32 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