Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select interval of time series object by date and time

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
like image 659
Leonardo Cantor Avatar asked Jan 26 '26 20:01

Leonardo Cantor


2 Answers

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)
like image 73
Joshua Ulrich Avatar answered Jan 28 '26 08:01

Joshua Ulrich


If your date-times are in a column called 'dtm' then this code should get the records that are within the interval 6A to 8A

dfrm <- data.frame(a=rnorm(24),  
                   dtm =as.POSIXct("2007-03-27 05:00", tz='GMT') +3600*(1:24) )     
    sub6_8A <- subset(dfrm, strftime(dtm, "%H", tz="GMT") %in% c('06','07','08') )
sub6_8A
           a                 dtm
1  0.5020823 2007-03-27 06:00:00
2 -0.7455312 2007-03-27 07:00:00
3  1.8035086 2007-03-27 08:00:00

You could also use an indexed approach with "[[", but if you have NA's they would get dragged along unless you specifically excluded them.

like image 35
IRTFM Avatar answered Jan 28 '26 09:01

IRTFM