Last active
April 8, 2022 08:48
-
-
Save rich-iannone/7551858 to your computer and use it in GitHub Desktop.
Several examples in R on creating subsets of data via a POSIXct time object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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