test test - 9 days ago 5
R Question

Subset specific dates (year and month) from data.frame

Here my data.frame:

df = read.table(text = 'ID Date
1 1975-01-01
2 1980-02-01
3 1985-05-01
4 1990-07-01
5 1990-08-01
6 1993-01-01
7 1993-09-01', header = TRUE)


I need to create a subset from it by selecting specific dates (year and month).

The dates I am interested are:

dates = c('1980-02', '1990-07', '1993-09')


and therefore my output should be:

ID Date
2 1980-02-01
4 1990-07-01
7 1993-09-01


Is there any possibility to write a unique code that do the trick starting from
df
and
dates
, without add manually the dates in the code?

Thanks

####UPDATE

What if I have got multiple observations in the same month within
df
, like:

df2 = read.table(text = 'ID Date
1 1975-01-01
2 1980-02-01
9 1980-02-01
3 1985-05-01
4 1990-07-01
12 1990-07-01
16 1990-07-01
5 1990-08-01
6 1993-01-01
7 1993-09-01
67 1993-09-01', header = TRUE)


New output:

ID Date
2 1980-02-01
9 1980-02-01
4 1990-07-01
12 1990-07-01
16 1990-07-01
7 1993-09-01
67 1993-09-01


Thanks

Answer

Here are some solutions. They (i) work with any dates, not just ones that are the first of the month, (ii) preserve the order of df2 in the output, (iii) are compact, i.e. one line each and do not require mentioning df2 multiple times.

1) Try this. No packages are used.

subset(df2, substr(Date, 1, 7) %in% dates)

giving:

   ID       Date
2   2 1980-02-01
3   9 1980-02-01
5   4 1990-07-01
6  12 1990-07-01
7  16 1990-07-01
10  7 1993-09-01
11 67 1993-09-01

2) Another possibility is to convert both Date and dates to "yearmon" class giving the same result. This code is a bit nicer but does need a package.

library(zoo)
subset(df2, as.yearmon(Date) %in% as.yearmon(dates))