1

My data looks like this,

       Date1        Date2       Date3
     13/12/2019   2018/05/21   21/05/2019
     2019/13/12   21/05/21     05/21/2019

The date format inside a variable and across variables are different. I want to convert it to as.Date as one common format "%d/%m%Y" for all variables at once. How do I do it?

Kindly help.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Jennifer Therese
  • 1,105
  • 9
  • 17

1 Answers1

1

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Okay. My data has also got categorical variables. Can I do this operation selecting only date type variables and apply parse_date_time? Is that possible? – Jennifer Therese Feb 20 '19 at 12:02
  • @JenniferTherese. Then use `mutate_at` with vars specied `mutate_at(vars(matches("Date\\d+")), parse_date_time, ...` – akrun Feb 20 '19 at 12:03