[R] help subsetting data based on date AND time

Steve E. searl at vt.edu
Thu Sep 8 22:03:02 CEST 2011

Dear R Community,

I am new to R, and have a question that I suspect may be quite simple but is
proving a formidable roadblock for me.  I have a large data set that
includes water-quality measurements collected over many 24-hour periods. 
The date and time of sample collection are in a combined Date/Time field in
the format yyyy-mm-dd hh:mm:ss.  I need to be able to subset the data for
analysis of different date and time windows.  Thus far, I have tried casting
the Date/Time field using several approaches, such as:

DataSet$NewDateTime <- strptime(DataSet$DateTime, '%Y-%m-%d %H:%M:%S')
DataSet$NewDateTime <- as.POSIXlt(strptime(DataSet$DateTime, '%Y-%m-%d

These instructions seem to cast the NewDateTime field correctly (at least it
appears to be in the correct format, and I assume R sees the field as a date
and a time) but I am then unable to subset the data using instructions such

with(DataSet, subset(DataSet, DataSet$NewDateTime < '2004-08-05 14:15:00'))
DataSubset <- subset(DataSet, DataSet$NewDateTime < '2004-08-05 14:00:00',
select = DataSet)

I have tried also separating the date and time fields in the input file, and
casting with instructions such as:

DataSet$NewTime <- strptime(DataSet$Time, '%H:%M:%S')
DataSet$NewTime <- as.POSIXct(strptime(DataSet$Time, '%H:%M:%S'))

but these seem to generate a NewTime field that contains today's date + the
time data, and also will not subset based on date/time.

I appreciate greatly any help and advice,

View this message in context: http://r.789695.n4.nabble.com/help-subsetting-data-based-on-date-AND-time-tp3799933p3799933.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list