Suehi
Suehi

Reputation: 1

how to change a date format in R

I need help in R program to change date format from "May 29, 2015" to "05,29,2015".I tried this "housing$SaleDate<- as.Date(housing$SaleDate,"%Y%M%D")" but did not work. Thanks

Upvotes: 0

Views: 62

Answers (1)

akrun
akrun

Reputation: 886948

After converting to Date class, we may need format i.e.

library(lubridate)
str1 <- "May 29, 2015" 
format(mdy(str1), '%m,%d,%Y')
[1] "05,29,2015"

For the OP's as.Date, it would be

housing$SaleDate <- format(as.Date(housing$SaleDate, "%b %d, %Y"), "%m,%d,%Y")

Upvotes: 2

Related Questions