Wboy Wboy - 25 days ago 13
R Question

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

I have this date column of form:

20160812
20160813


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!

J_F J_F
Answer

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)
Comments