[R-SIG-Finance] irregular time series to regular time series

Pete Brecknock Peter.Brecknock at bp.com
Tue Dec 20 03:13:00 CET 2011


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
> 
> HI,
>  
> I have an irregular time series data (contains time and price) of a few
> days like this. I want to change it to a regular equally time spaced
> series. Take the time interval 5min as an example. Then the equally time
> spaced series start from 9am to 11am and contiue from 12:20pm to 15:15  by
> a 5min interval.  Can you help me with that? Thanks.   
> 
>                           time                    price
> 48721  2011-10-12 15:09:21 6.626387
> 48722  2011-10-12 15:09:22 6.625913
> 48723  2011-10-12 15:09:42 6.625724
> 48724  2011-10-12 15:09:48 6.625724
> 48725  2011-10-12 15:10:14 6.625724
> 48726  2011-10-12 15:10:29 6.625724
> 48727  2011-10-13 10:00:03 6.635290
> 48728  2011-10-13 10:00:04 6.635618
> 48729  2011-10-13 10:00:05 6.635618
> 48730  2011-10-13 10:00:06 6.635618
> 48731  2011-10-13 10:00:07 6.635618
> 48732  2011-10-13 10:00:09 6.635618
> 48733  2011-10-13 10:00:10 6.635618
> 48734  2011-10-13 10:00:11 6.634962
> 48735  2011-10-13 10:00:12 6.634962
> 48736  2011-10-13 10:00:13 6.634962
> 48737  2011-10-13 10:00:14 6.635181
> 48738  2011-10-13 10:00:15 6.635618
> 


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



More information about the R-SIG-Finance mailing list