Reputation: 13
I've been trying to figure out how to use the lubridate & tidyverse package to filter for a specific hour (2pm) in my dataset within the subset of the month of January. The dataset itself has 4 years of meteorological data and I'm trying to find the mean daily AirTemp at 2pm in January for the 4 years
my data currently looks like this:
Data
structure(list(Timestamp = c("2010-01-01 01:00:00", "2010-01-01 02:00:00",
"2010-01-01 03:00:00", "2010-01-01 04:00:00", "2010-01-01 05:00:00",
"2010-01-01 06:00:00"), Temp = c(44L, 44L, 44L, 44L, 43L, 42L
), Humid = c(100L, 96L, 93L, 89L, 89L, 83L), Precip = c(0L, 0L,
0L, 0L, 0L, 0L)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6"))
Timestamp Temp Humid Precip
1 2010-01-01 01:00:00 44 100 0
2 2010-01-01 02:00:00 44 96 0
3 2010-01-01 03:00:00 44 93 0
4 2010-01-01 04:00:00 44 89 0
5 2010-01-01 05:00:00 43 89 0
6 2010-01-01 06:00:00 42 83 0
I've tried writing my filters so many time but there seems to be a misunderstanding in the way I'm writing my syntax I think
e.g.
weather %>%
mutate(gethour = hour(weather$Timestamp)) %>%
filter(gethour == gethour("2:00 PM"))
every time I try to write a simple filter I get errors, does anyone know what Im doing wrong?
Upvotes: 0
Views: 1569
Reputation: 388807
To get data for 2pm in the month of January for all years you can use :
library(dplyr)
library(lubridate)
weather %>% filter(hour(Timestamp) == 14 & month(Timestamp) == 1)
Similarly in base R, we can use subset
:
subset(weather,format(Timestamp, "%H") == "14" & format(Timestamp,, "%m") == "01")
Upvotes: 3