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$ Commented Jul 8, 2020 at 15:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.