Extracting Rows from a Dateframe by Hour: A Simple R Example
library(lubridate)
df$time <- hms(df$time)  # Convert to time class
df$hour <- hour(df$time)  # Extract hour component

# Perform subsetting for hours 7, 8, and 9 (since there's no hour 10 in the example data)
df_7_to_9 <- df[df$hour %in% c(7, 8, 9), ]

print(df_7_to_9)

This will print out the rows from df where the hour is between 7 and 9 (inclusive). Note that since there’s no row with an hour of 10 in your example data, I’ve adjusted the condition to include hours 8 as well. If you want to keep the original condition and just exclude any rows with a non-existent hour 10, you can do so by changing c(7, 8, 9) to c(7, 8).


Last modified on 2025-02-27