2
$\begingroup$

I have a R dataframe having a column with dates in multiple formats. I want to convert this column to date format.

While some formats are converted others are not. Why is the second date not converted? How can this be corrected?

Code and result:

> slno <- c(1,2)
> Date <- c('22-04-2015', '4/8/2015')
> 
> df_data <- as.data.frame(cbind(slno,Date))
> df_data
  slno       Date
1    1 22-04-2015
2    2   4/8/2015
> 
> date_formats <- c('%m/%d/%Y', '%d-%m-%Y')
> 
> #df_data$Date <- mdy_hm(df_data$Date)
> df_data$Date <- as.Date(as.character(df_data$Date), tryFormats = date_formats, optional = F)
> 
> df_data
  slno       Date
1    1 2015-04-22
2    2       <NA>
> str(df_data)
'data.frame':   2 obs. of  2 variables:
 $ slno: chr  "1" "2"
 $ Date: Date, format: "2015-04-22" ...
> 
$\endgroup$

1 Answer 1

1
$\begingroup$

The parse_date_time function from the lubridate package will do this. This function takes an argument orders which is like the tryFormats argument in 'as.Date', except the formats allowed are more generous and lubridate-like (without punctuation, delimiters, etc.). For example, you can set orders = c("dmy", "mdy").

Date <- c('22-04-2015', '4/8/2015')
parse_date_time(Date, orders = c("dmy", "mdy"))
"2015-04-22 UTC" "2015-08-04 UTC"
$\endgroup$
1
  • $\begingroup$ Thank you very much for the help. The parsing was successful. A small glitch though: I have the dates "22-04-2015" "4/8/2015". After parsing: "2015-04-22 UTC" "2015-08-04 UTC". The second one should be 2015-04-08. I used orders=c("dmy", "mdy") as the first date is dd-mm-yyyy and the second one is mm/dd/yyyy. Also anyway to get rid of the UTC? $\endgroup$
    – S Datta
    Jul 8, 2020 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.