Reputation: 653
I have a dataframe in which dates stored in one column. But the problem is some values are stored using %d-%m-%y format while the others stored in %Y-%m-%d format. I want all the values in one format, so I tried
df$dateCol= as.Date(df$dateCol, format = "%d-%m-%y")
But it didn't format the values properly
What's the correct way to solve this issue?
Upvotes: 0
Views: 53
Reputation: 93813
You could do a simple ifelse()
checking where the dashes -
are:
x <- c("07-06-2016","2016-06-07","2015-01-01")
as.Date(x, ifelse(substr(x, 5, 5)=="-", "%Y-%m-%d", "%d-%m-%Y") )
#[1] "2016-06-07" "2016-06-07" "2015-01-01"
Upvotes: 2