0

I have a character vector with timestamps like the following:

Date <- c("Jul 31, 2017", "Jul 30, 2017", "Jul 29, 2017", "Jul 28, 2017")

And I'm trying to convert it into a date object type. I have tried several methods:

as.Date

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

strptime

Date <- strptime(Date, format = "%B %d, %Y")

mdy from lubridate

Date <- mdy(Date)

But all of them returns NAs. I have also tried changing the format argument from %B to %b and the result is the same. Moreover, with mdy from lubridate parses the data in a different format than I'm expecting to. For example, if I do the run the next piece of code:

mdy("Jul 01, 2017")

I get as a result:

[1] "2017-01-20"

What can I do to convert it to a date in the proper format? Thank you.

rubengura
  • 459
  • 1
  • 3
  • 18
  • 3
    It is all working fine with me `as.Date(Date, format = "%B %d, %Y") #[1] "2017-07-31" "2017-07-30" "2017-07-29" "2017-07-28"` – akrun Dec 05 '17 at 13:47
  • It's weird, I get NA as a result. as.Date(Date, format = "%B %d, %Y") [1] NA NA NA NA – rubengura Dec 05 '17 at 13:53
  • Can u try on a fresh session. BTW, I am using R 3.4.2 – akrun Dec 05 '17 at 13:53
  • 1
    Possibly a `locale` issue? [strptime, as.POSIXct and as.Date return unexpected NA](https://stackoverflow.com/questions/13726894/strptime-as-posixct-and-as-date-return-unexpected-na) – Henrik Dec 05 '17 at 14:01
  • Yeah, works fine for me too .... – sorearm Dec 05 '17 at 14:35
  • Yes, it was a `locale` problem. My locale was "Spanish_Spain.1252" so, after changing it to "C" it works perfectly. Thank you! – rubengura Dec 05 '17 at 14:40

0 Answers0