sethparker
sethparker

Reputation: 85

Filtering daily data over many years for a specific month(s) in R

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

Answers (2)

akrun
akrun

Reputation: 886948

An option is also to use %b

dates[format(dates, "%b) == "Jul"]

Upvotes: 0

Gregor Thomas
Gregor Thomas

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

Related Questions