test test - 29 days ago 12
R Question

Subset specific months within a data.frame which contains different years

Here a sample of my data.frame:

df = read.table(text = 'Date ID
1972-10-01 1
1972-12-01 2
1973-02-01 6
1973-04-01 1
1973-05-01 7
1973-09-01 9
1974-01-01 3
1974-02-01 8
1974-03-01 2
1975-01-01 1
1976-03-01 8
1976-12-01 9, header = TRUE)


I need to subset my data.frame in order to have only observations within the months: 12 (Dec), 01 (Jan) and 02 (Feb) for all the different years.

Here my desired output:

Date ID
1972-12-01 2
1973-02-01 6
1974-01-01 3
1974-02-01 8
1975-01-01 1
1976-12-01 9


The column
Date
is class = Date and my real data.frame contains > 50,000 rows and 9 columns, within 40 years of period.

Please help.
Thanks

J_F J_F
Answer

My approach would be this:

library(lubridate)

df[month(df$Date) %in% c(12,1,2),]
#         Date ID
#2  1972-12-01  2
#3  1973-02-01  6
#7  1974-01-01  3
#8  1974-02-01  8
#10 1975-01-01  1
#12 1976-12-01  9

This will work, when your column "Date" is really of class Date. It is possible with base R to extract the month, but in my opinion the lubridate solution is the most intuitive.

If column Date is not of class Date, then do the following before executing the code above:

df$Date <- as.Date(df$Date)
Comments