[R] Select interval of time series object by date and time

Rui Barradas ruipbarradas at sapo.pt
Sat Apr 21 00:23:07 CEST 2012


Hello,


florian wrote
> 
> Hy,
> 
> I want to select an interval of a time series (containing intraday data)
> by the data and a certain time frame (e.g. 9 AM to 10 AM). However, I do
> not know how to specify the time during the day:
> 
> #load data
> library(RTAQ)
> data("sample_tdata")
> data=sample_tdata
> return=makeReturns(data$PRICE)
> 
> #I would like to use some command like this to get only the data between 9
> and 10
> window(return,start=as.Date("2008-01-04"),end=as.Date("2008-01-04"))
> 
> Thank you very much in advance
> Florian Walla
> University Groningen
> 

If you're working with time series, sooner or later you'll want package
'zoo'.
This example uses it.


library(zoo)
set.seed(1)
# Make some data
x <- as.POSIXct("2008-01-04 9:00:00")+ cumsum(sample(200, 100, T))
y <- rnorm(100)

start.hour <- as.POSIXct("2008-01-04 9:00:00")
end.hour <- as.POSIXct("2008-01-04 10:00:00")

# Create the 'zoo' object and extract the desired window
Z <- zoo(y, order.by=x)
w <- window(Z, start=start.hour, end=end.hour)
# Compute some statistics
sum(w)
mean(w)
sd(w)

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Select-interval-of-time-series-object-by-date-and-time-tp4573220p4575440.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list