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. thanks -- View this message in context: http://r.789695.n4.nabble.com/working-with-zoo-time-index-tp2255804p2255804.html Sent from the R help mailing list archive at Nabble.com.
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))
Hi thanks Let say data are written like this: 1990-01-01 10:01:00 , 0.910 1990-01-01 10:03:00 , 0.905 Would it be ok to read it with theses lines or is better to use your way? tmp <- read.table("demo2.txt", sep = ",") z <- zoo(tmp[, 2], as.Date(as.chron(tmp[, 1]), format = "%Y-%m-%d %H:%M:%S")) regards -- View this message in context: http://r.789695.n4.nabble.com/working-with-zoo-time-index-tp2255804p2257222.html Sent from the R help mailing list archive at Nabble.com.
On Wed, Jun 16, 2010 at 1:10 PM, skan <juanpide at gmail.com> wrote:> > I said taking the first element everyday, but that was just an example, I > could need one every 2 hours or something more complicated such as one every > hour if the former one was non null.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) z <- read.zoo(textConnection(Lines), sep = ",", tz = "") # take mean of every 2 hour segment aggregate(z, as.POSIXct(cut(time(z), "2 hours", include = TRUE)), mean) For more examples, see: ?aggregate.zoo
On Wed, Jun 16, 2010 at 4:55 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Wed, Jun 16, 2010 at 1:10 PM, skan <juanpide at gmail.com> wrote: >> >> I said taking the first element everyday, but that was just an example, I >> could need one every 2 hours or something more complicated such as one every >> hour if the former one was non null. > > 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) > z <- read.zoo(textConnection(Lines), sep = ",", tz = "") > > # take mean of every 2 hour segment > aggregate(z, as.POSIXct(cut(time(z), "2 hours", include = TRUE)), mean) > > For more examples, see: > ?aggregate.zoo >And here it is using chron: 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) # take mean of every 2 hour segment aggregate(z, trunc(time(z), "02:00:00"), mean)
thanks How can I do it without using "aggregate"? In other languages they use commands like Time[i] or Date[i]<>Date[i-1] were i is the cell -- View this message in context: http://r.789695.n4.nabble.com/working-with-zoo-time-index-tp2255804p2257952.html Sent from the R help mailing list archive at Nabble.com.