One option would be parse_date_time
library(tidyverse)
library(lubridate)
df1 %>%
mutate_all(parse_date_time, orders = c("mdy", "ymd", "dmy", "ydm"))
# Date1 Date2 Date3
#1 2019-12-13 2018-05-21 2019-05-21
#2 2019-12-13 2021-05-21 2019-05-21
or another option is anydate
from anytime
. Check whether all the formats are there in the default format list with getFormats()
and if some are missing, add those with addFormats
and then apply on each column
library(anytime)
addFormats(c("%d/%m/%Y", "%Y/d/%m", "%d/%m/%y"))
df1 %>%
mutate_all(anydate)
If there are more columns and want to implement only for 'Date' columns, use mutate_at
df1 %>%
mutate_at(vars(starts_with("Date")), anydate)
Or specify the index (in the original dataset)
df1 %>%
mutate_at(4:6, anydate)
data
df1 <- structure(list(Date1 = c("13/12/2019", "2019/13/12"), Date2 = c("2018/05/21",
"21/05/21"), Date3 = c("21/05/2019", "05/21/2019")),
class = "data.frame", row.names = c(NA,
-2L))