R: Selecting datetime subset with "YYYY-MM" condition

646 views Asked by At

My data is structured as follows:

        curr      time
        <chr>   <date>
1       USD 2015-07-18
2       USD 2014-10-16
3       USD 2016-03-26

Question:

I like to select the full month

subset(ks, deadline >= '2010-01' & deadline <= '2016-03')

This returns

Error in charToDate(x) : 
  character string is not in a standard unambiguous format.

This works, but would always need a manual check of the days of a months.

subset(ks, deadline >= '2010-01-01' & deadline <= '2016-03-31')

Is there a way to get the first "error" version working?

2

There are 2 answers

0
joel.wilson On

I only have a long approach here ! The check condition shall have 3 parts :

  1. All years with all months within the interval : eg. in our case years 2010 to 2015 all months are considered.

  2. The last year : May be only few months are involved here .eg From 2016 , only first 3 months are considered. Same for the starting year

    library(lubridate)
    log.cond <- (year(dt$time) %in% 2010:2015) | (year(dt$time) == 2016 & month(dt$time) %in% 1:3)
    subset(dt, log.cond)
    
2
agenis On

It seems that all your dates and deadlines are in character format. The best is to use the date formats (like the very useful family of functions ymd, ymd_hms, year, month etc. from the lubridate package) But if they are in the english-speaking order (year first, then month, then day, with leading zeros), you don't actually need to turn them into dates to subset, you can leave everything in text format, cut the last 3 characters (days) and R will make numeric comparisons:

ks = data.frame(curr="USD", "time"=c("2015-07-18", "2014-10-16", "2016-03-26"), stringsAsFactors = F)
ks$time2 <- substr(ks$time, 1, nchar(ks$time)-3)

Then you can use your first syntax without any change:

subset(ks, time2 >= '2015-01' & time2 <= '2016-03')
####   curr       time   time2
#### 1  USD 2015-07-18 2015-07
#### 3  USD 2016-03-26 2016-03