Select interval of time series object by date and time

Leonardo Cantor picture Leonardo Cantor · Jun 16, 2012 · Viewed 8.9k times · Source

My question is about how to manage the dates and times in an air quality database, which saved data every ten minutes all day, every day from 2002 through 2008.

I want to generate several analysis and plots, but referring only to the morning peak hours which go from 6:00 through 8:00 a.m. I have tried to generate the diagrams in the needed interval but the R tool always plots the 24 hours in a day distorting, therefore, the available data for the peak hours.

I would hugely appreciate your guidance on how to select and plot interval in the peak hour only and how to generate the several diagrams.

I have the next script to generate a date interval, but I want to agregate hour interval (6-8 am) and plot only the interval data:

# select interval
start.date = as.POSIXct("2007-03-27 05:00", tz = "GMT")
end.date = as.POSIXct("2007-05-27 05:00", tz = "GMT")
subdata = subset(mydata, date >= start.date & date <= end.date,
select = c(date, nox, co))
#
#plot the variables

Answer

Joshua Ulrich picture Joshua Ulrich · Jun 16, 2012

I recommend you use a time series class instead of a data.frame. Subsetting by a time interval each day is easy with xts:

# use DWin's example data
Data <- data.frame(a=rnorm(240),
  dtm=as.POSIXct("2007-03-27 05:00", tz="GMT")+3600*(1:240))
# create xts object
library(xts)
x <- xts(Data[,"a"], Data[,"dtm"])
# subset by time of day
y <- x["T06:00/T08:00"]
# plot
plot(y)  # plots all 24 hours of each day
# use chartSeries from quantmod to avoid above behavior
library(quantmod)
chartSeries(y)