1

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?

agenis
  • 8,069
  • 5
  • 53
  • 102
Fanny
  • 55
  • 11
  • You could add a new column `ks$time2 <- strftime(ks$time, "%Y-%m")` and then apply your filter to that column. `subset(ks, time2 >= "2010-01" & time2 <= "2016-03"` – count Aug 31 '17 at 11:27
  • @count I had only one question here : strftime returns a `character` . So would it be right to compare keeping it as `strings` ? Just wanted to learn – joel.wilson Aug 31 '17 at 11:31
  • @Fanny Hi, if any answer solves your problem can you click on "accept it" so that other people can see it? thanks – agenis Sep 06 '17 at 12:52

2 Answers2

1

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)
    
joel.wilson
  • 8,243
  • 5
  • 28
  • 48
0

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
agenis
  • 8,069
  • 5
  • 53
  • 102
  • 1
    Just a minor correction The format `YYYY-MM-DD` is one of the ISO 8601 date formats, ISO 8601 has been adopted by many countries worldwide but still local date formats are in widespread use. E.g., in the US which is one of the major English-speaking areas, the format MM/DD/YYYY is preferred. – Uwe Aug 31 '17 at 12:31
  • the `substr` suggested here can also be replaced by `strptime` suggested in a comment – agenis Aug 31 '17 at 12:33