Wboy Wboy - 4 months ago 46
R Question

as.Date showing character string in unambiguous format even when strptime specified R

I have this date column of form:


Basically YYYYMMDD.

I converted it to a date using strptime and as.Date

weather_dataset$DATE = as.Date(weather_dataset$DATE,"%Y%m%d")

But I get

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

Huh? I've clearly specified the format to use, but its still throwing me this error for some reason. Would appreciate help! :)

Thank you!


Your data has to be a character. So use this workaround:

weather_dataset$DATE <- as.Date(as.character(weather_dataset$DATE),"%Y%m%d")

Another possibility is the library(lubridate):

weather_dataset$DATE <- lubridate::ymd(weather_dataset$DATE)