Skip to content
Prev 9141 / 15274 Next

irregular time series to regular time series

Guessing a little bit as I don't fully undrstand your question but how about
the aggregate function in zoo?

library(zoo)

# your data
Lines <- "date time price
2011-10-12 15:09:21 6.626387
2011-10-12 15:09:22 6.625913
2011-10-12 15:09:42 6.625724
2011-10-12 15:09:48 6.625724
2011-10-12 15:10:14 6.625724
2011-10-12 15:10:29 6.625724
2011-10-13 10:00:03 6.635290
2011-10-13 10:00:04 6.635618
2011-10-13 10:00:05 6.635618
2011-10-13 10:00:06 6.635618
2011-10-13 10:00:07 6.635618
2011-10-13 10:00:09 6.635618
2011-10-13 10:00:10 6.635618
2011-10-13 10:00:11 6.634962
2011-10-13 10:00:12 6.634962
2011-10-13 10:00:13 6.634962
2011-10-13 10:00:14 6.635181
2011-10-13 10:00:15 6.635618"

# read in data
d <- read.table(textConnection(Lines), header = TRUE)
closeAllConnections()

# create date time field
d$datetime <-as.POSIXct(paste(d$date,d$time,sep=" "), origin="1970-01-01")

# create zoo object
d.z = zoo(d$price,order.by=d$datetime)

# aggregate data by 5 min period
aggregate(d.z, time(d.z) - as.numeric(time(d.z)) %% 300, mean)

# output
2011-10-12 15:05:00 2011-10-12 15:10:00 2011-10-13 10:00:00 
           6.625937            6.625724            6.635390 

HTH

Pete


newnew wrote
--
View this message in context: http://r.789695.n4.nabble.com/irregular-time-series-to-regular-time-series-tp4214779p4216416.html
Sent from the Rmetrics mailing list archive at Nabble.com.