Skip to content

Instantly share code, notes, and snippets.

@mercelsantos
Forked from rich-iannone/subset.POSIXct.R
Created July 12, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mercelsantos/9eca4733356f9d5981d05dc394c445bc to your computer and use it in GitHub Desktop.
Save mercelsantos/9eca4733356f9d5981d05dc394c445bc to your computer and use it in GitHub Desktop.
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