[R] working with zoo time index ??

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 16 21:41:13 CEST 2010


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))



More information about the R-help mailing list