[R] working with zoo time index ??

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jun 15 15:30:39 CEST 2010


On Tue, Jun 15, 2010 at 8:27 AM, skan <juanpide at gmail.com> wrote:
>
> Hello
>
> Where could I find examples on how to work with the time index in a
> timeseries  or zoo series?
>
> Let say I've got this series
>
> DATA
> 1990-01-01 10:00:00   0.900
> 1990-01-01 10:01:00   0.910
> 1990-01-01 10:03:00   0.905
> 1990-01-01 10:04:00   0.905
> 1990-01-01 10:05:00   0.890
>
> .......................
>
> 2000-12-31 20:00:00   0.992
>
>
> How do I make simple calculations such as ... ?
> Calculate the mean of the first data every day. (mapply, for loop, tapply ?)
> Transform data to a table,  with dates in one axis and  times in the other.
>

There are three vignettes that come with zoo.  vignette() lists their
names and vignette("zoo") displays the one called zoo (similarly for
the other two).  Also see the help files: ?zoo, ?read.zoo,
?aggregate.zoo
and note the examples at the bottom of the help files.
Also library(help = zoo) lists the help files available.

Lines <- "1990-01-01 10:00:00   0.900
1990-01-01 10:01:00   0.910
1990-01-01 10:03:00   0.905
1990-01-01 10:04:00   0.905
1990-01-01 10:05:00   0.890
1990-01-02 10:00:00   0.940
1990-01-02 10:01:00   0.990"
library(zoo)
library(chron)
z <- read.zoo(textConnection(Lines), index = 1:2, FUN = function(x)
as.chron(paste(x[,1], x[,2])))

# take first data value for each day and then take their mean
mean(aggregate(z, as.Date, head, 1))

# create data frame from z made up of dates, times and value
# dates and times are chron package functions.
# (If you use a different date and time class then it would be different.)
data.frame(dates = dates(time(z)), times = times(time(z)), value = coredata(z))



More information about the R-help mailing list