How can I subset a data frame by a specific year and two specific months?

103 views Asked by At

I have been subsetting my data set based on year and month using:

sub2007 <- subset(Data, format(Date, "%Y") == "2007" & format(Data, "%m") == "04")

Which has been working fine. However, how can I select multiple months? I have tried several things but it keeps giving me a data frame with zero obs. Any help welcome.

I have tried:

sub2007 <- subset(Data, format(Date, "%Y") == "2007" & format(Date, "%m") == "04" & format(Date, "%m") == "05")

and

2007sub <- subset(Data, as.Date(Date) >= "01/04/2007" & as.Date(Date) < "01/06/2007")

Ideally I would like to select individual months, and not a between two dates type command.

Data looks like:

| ID        | Date       | Between    |
--------------------------------------
| D12222222 | 30/11/2007 | 0          |
| D12222222 | 02/04/2007 | 278        |
| F33333333 | 24/05/2007 | 0          |
| F33333333 | 06/05/2007 | 5279       |
| F33333333 | 30/04/2007 | 267        |
0

There are 0 answers