Reputation: 1116
I have monthly data
library(zoo); library(data.table)
dat <- data.table(month=as.yearmon(paste0("2019-0",1:9)))
> dat
month
1: Jan 2019
2: Feb 2019
3: Mrz 2019
4: Apr 2019
5: Mai 2019
6: Jun 2019
7: Jul 2019
8: Aug 2019
9: Sep 2019
and I would like to subset it to months after, say, May 2019. Whereas
dat[month=="Jun 2019"]
works,
> dat[month > "May 2019"]
Empty data.table (0 rows and 1 cols): month
> dat[month%in%c("Jun 2019","Jul 2019")]
Empty data.table (0 rows and 1 cols): month
do not. Is there a trick?
Upvotes: 0
Views: 359
Reputation: 269461
Actually it does work. Fixing the double quotes we have:
library(data.table)
library(zoo)
dat <- data.table(month=as.yearmon(paste0("2019-0",1:9), "%Y-%m"))
dat[month > "May 2019"]
## month
## 1: Jun 2019
## 2: Jul 2019
## 3: Aug 2019
## 4: Sep 2019
The comparison will use the language currently in effect. For example, if we set the current session to French:
Sys.setlocale(locale = "French")
dat[ month > "mai 2019" ]
## month
## 1: juin 2019
## 2: juil. 2019
## 3: août 2019
## 4: sept. 2019
We can also do it in a language independent way by using month numbers rather than names:
dat[ month > "2019-05" ]
## month
## 1: Jun 2019
## 2: Jul 2019
## 3: Aug 2019
## 4: Sep 2019
It would be good enough to do this to create the example:
dat <- data.table(month = as.yearmon(paste0("2019-", 1:9)))
Upvotes: 1