Reputation: 85
I have daily data for a sequence of dates
dates <- seq(as.Date("1915/01/01"),as.Date("2016/12/31"),"day")
> length(dates)
[1] 37256
How would I go about pulling out July for every year? I know how to subset, but would prefer not to have a separate object for each year.
Upvotes: 0
Views: 129
Reputation: 145755
You can do this by extracting the month and filtering on that:
# base R
dates[format(dates, "%m") == "07"]
dates[format(dates, "%B") == "July"] # locale-dependent
# lubridate
library(lubridate)
dates[month(dates) == 7]
Upvotes: 1