I am trying to convert the following format to date:
as.Date('Mar.17', format = '%b.%y')
but it returns NA.
What am I missing?
Update, I am expecting to get March 2017, not 2018
it should be:
as.Date('Mar.17', format = '%b.%d')
Assuming the 17
part is the year, you could use sub
to add in a day number to make it an actual date.
as.Date(sub("\\.", "01", "Mar.17"), "%b%d%y")
# [1] "2017-03-01"
as.yearmon
from zoo
package will do the trick and provide date(Mar 2017)
as expected by OP.
library(zoo)
as.yearmon("Mar.17", "%b.%y")
#[1] "Mar 2017"
Another option to convert it to 1 March 2017
as.Date(as.yearmon("Mar.17", "%b.%y"), frac = 0)
#[1] "2017-03-01"
You need the point for %b month format, at least in my computer
as.Date(paste0( "01",'mar.2017'), format = '%d%b%Y')
"2017-03-01"
as.Date(paste0('Mar.17',1), format = '%b.%y%d')