Valentina Ruts
Valentina Ruts

Reputation: 121

No way to solve as.Date returning NA

R Studio beginner here. I am trying to turn a character into a date through as.Date. My code is like this:

date=c("23-Nov-1994")

date <- as.Date(date, format="%Y%m%d")

but it returns NA.

I also tried

as.POSIXlt("23-11-1994", format="%d-%m-%Y")

which returns [1] NA

The locale shouldn't be the issue:

Sys.getlocale("LC_TIME")

returns [1] "English_United States.1252"

Finally, I tried to use lubridate as someone suggested in another thread but R returns that there is no package called lubridate! I really don't understand why, can anyone help? R version is 3.4.4.

Thank you!

Upvotes: 3

Views: 8738

Answers (1)

akrun
akrun

Reputation: 887088

The format should match the order, %m is for numeric months

as.Date(date, format="%d-%b-%Y")
#[1] "1994-11-23"

Also, this can be done with lubridate

library(lubridate)
dmy(date)
#[1] "1994-11-23"

Or with anytime

anytime::anydate(date)
#[1] "1994-11-23"

Upvotes: 5

Related Questions