1

I need your help to figure out the following problem-

I am trying to convert a date column from string to actual date format. I have tried using as.Date

However, it is showing an error message: Error in charToDate(x) : character string is not in a standard unambiguous format

the date column I have in csv file is like this:

Date

03/17/2003

05/31/2003

09/06/2003

10/18/2003

07/15/2003

09/19/2003

The problem is some of the dates are in string and some are in actual date format. I have tried to format it from excel - didn't work Tried to copy and paste it to notepad and then import it again - didn't work either.

1 Answers1

4

You need to learn about the help system in R. One brief look at help(as.Date) may have told you about the format argument:

R> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
R> as.Date(dt, "%m/%d/%Y")
[1] "2003-03-17" "2003-05-31" "2003-09-06"
R> 

Edit: These days we also have a helper package that does the format-finding for you:

> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
> anytime::anydate(dt)
[1] "2003-03-17" "2003-05-31" "2003-09-06"
> 

This works for datetimes (using anytime()) and dates.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725