0

I have a large dataset with character values that look like dates but they're in two different formats. For example:

input <- c("2019-01-22", "2019-04-17", "05/17/2009", "05/17/2010", "05/17/2015", "2019-07-30")

How would I change "05/17/2009", "05/17/2010", "05/17/2015" (anything else in this format) into the same "%Y-%m-%d" format as the others in the list? I also want to convert everything into a date.

The problem I have is that the placement of the year, month, and day are different so I can't just rplace "/" with a "-".

1 Answers1

1

Try one format and then the other for the values that returned NA.

y <- as.Date(input)
na <- is.na(y)
y[na] <- as.Date(input[na], "%m/%d/%Y")
y
#[1] "2019-01-22" "2019-04-17" "2009-05-17" "2010-05-17" "2015-05-17"
#[6] "2019-07-30"
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66