I have a data set that has a column for dates (emp2$hiredate) which is in char format. While converting it to Date, I'm getting the following error :
> date1 <- emp2$hiredate[2]
> str(date1)
chr "20FEB1981"
> as.POSIXct(date1)
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
How do I solve this problem?
R doesn't automatically know the format of the date in the string. You need to use the
format
argument to tell it how to process the string.You can use
as.Date
to create a dateOr use
as.POSIXct
with the format option to create a date-time%d
means the day of the month in decimal form%b
means that the month is not in numeric form, but rather an abbreviated name%Y
(capitalized) means the year is in century form (all four numbers)