0
votes

I'm using the code below to try to change the date format of a file that I imported from a CSV file. It works fine and returns dates like "2013-09-21". However, I want it to return 9-21-2013. I tried changing the format to Y/d/m, but it returns all NAs. How can I fix this?

web <- read.csv("Web.csv", header = TRUE)
as.Date(web$CLAIM.DATE, format = "%m/%d/%Y")

Also, on a related note, how can I replace the column in the web data set with the values from as.date (when I get it correct)? Thanks!!

1
The format argument is telling as.Date() the current format so it knows how to correctly read the date in. It is not telling it the format you want displayed.Gregor Thomas
Since your data example seems to be the format Year-Month-Day, it's not wonder that you get NA with Y/d/m.Gregor Thomas

1 Answers

0
votes
> mydate <- "2013-09-21"
> as.Date(mydate)
[1] "2013-09-21"
> format(as.Date(mydate), '%m/%d/%Y')
[1] "09/21/2013"

And if you want to replace the column you can use:

web$CLAIM.DATE <- format(as.Date(web$CLAIM.DATE), '%m/%d/%Y')