Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Several examples in R on creating subsets of data via a POSIXct time object.
# Create a simple data frame for testing
df <- data.frame(POSIXtime = seq(as.POSIXct('2013-08-02 12:00'),
as.POSIXct('2013-08-06 05:00'), len = 45),
x = seq(45))
# The Subset Examples
#
# All data on 2013-08-06
sub.1 <- subset(df, format(POSIXtime,'%d')=='06')
# All data on or before the 2013-08-04 at 5 PM
sub.2 <- subset(df, POSIXtime <= as.POSIXct('2013-08-04 17:00'))
# All data in the hours of 12 PM through 4 PM inclusive
sub.3 <- subset(df, format(POSIXtime, '%H') %in% c('12', '13', '14', '15', '16'))
# All data from 6 AM through 1:45 AM on the 2013-08-05
sub.4 <- subset(df, POSIXtime >= as.POSIXct('2013-08-05 06:00') &
POSIXtime <= as.POSIXct('2013-08-05 13:45'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment