I have some data that look something like this:
require(zoo) X <- rbind(c(date='20111001', fmt='%Y%m%d'), c('20111031', '%Y%m%d'), c('201110', '%Y%m'), c('102011', '%m%Y'), c('31/10/2011', '%d/%m/%Y'), c('20111000', '%Y%m%d')) print(X)
I want only a year and a month. I donβt need this day, so I donβt worry that the last day is not valid. R, unfortunately, is:
mapply(as.yearmon, X[, 'date'], X[, 'fmt'], SIMPLIFY=FALSE) # $`20111001` # [1] "Oct 2011" # $`20111031` # [1] "Oct 2011" # $`201110` # [1] "Oct 2011" # $`102011` # [1] "Oct 2011" # $`31/10/2011` # [1] "Oct 2011" # $`20111000` # Error in charToDate(x) : # character string is not in a standard unambiguous format
I know that the usual answer is to fix the daytime part of the date, for example. using paste(x, '01', sep='') . I donβt think this will work here because I donβt know in advance what the date format will be, and therefore I cannot set the day without converting to some date object.
source share