0

I'm trying to format dates with R. Can someone explain why R gives an error when these two strings are the exact same format? I cannot figure out how to manage these date strings, and any help is greatly appreciated as I'm new working with R.

> as.Date("09/18/2016 1:00 PM EDT")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
> as.Date("09/08/2016 8:30 PM EDT")
[1] "0009-08-20"

bad  <- as.Date("09/18/2016 1:00 PM EDT")
good <- as.Date("09/08/2016 8:30 PM EDT")
MostlyRquestions
  • 526
  • 2
  • 7
  • 22
  • The 2nd conversion was also wrong -- it says the year is 9 AD. The easiest way to deal with dates is to use `lubridate`. – Hack-R Sep 12 '16 at 21:52
  • You need to specify the format, see `?as.Date`. E.g: `as.Date(dates, format = "%m/%d/%y")` – Jaap Sep 12 '16 at 21:52

1 Answers1

2

You missed the fact (and warning) that a non-standard format needs a format string for as.Date(). And you also want strptime() if you want parse hours/minutes etc:

R> strptime("09/18/2016 1:00 PM EDT", "%m/%d/%Y %I:%M %p")
[1] "2016-09-18 13:00:00 CDT"
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725