Search code examples
rdatefiltersubsetposixct

Create two data frames based on specified months


I have a annual data set, and I would like to obtain a data set with just the winter months (12, 01, 02) and summer months (06,07,08). How would I go about doing this?

#Example data set
library(lubridate)
date <- rep_len(seq(dmy("26-12-2010"), dmy("20-12-2011"), by = "days"), 500)
df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000))

Solution

  • You can pull the month out using month():

    df$month <- month(df$date)
    

    And then to filter:

    summer <- df %>% filter(month %in% 6:8)
    winter <- df %>% filter(month %in% c(12,1,2))