Jack
Jack

Reputation: 167

Convert date format to appropriate one?

I have column filled with date with the following format 09nov1992 and want convert it to 1992-Nov-01.

Any help would be appreciated.

Upvotes: 2

Views: 195

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

Here's a simple way:

vec <- "09nov1992"

format(as.Date(vec, "%d%b%Y"), "%Y-%b-%d")
# [1] "1992-Nov-09"

An alternative version using regular expressions:

sub("(\\d+)(\\w)(\\w+?)(\\d+)", "\\4-\\U\\2\\L\\3-\\1", vec, perl = TRUE)
# [1] "1992-Nov-09"

Upvotes: 3

Related Questions