Search code examples
rdatelubridate

Convert Date in R from dash to slash format?


I have the following dates:

my_dataset <- tibble(my_date = c("03-05-2020", "04-05-2020", "05-05-2020", "06-05-2020"))

I would like to create a new column called nice_dates which has the date format as: dd/mm/yyyy format, so I would end up with something like this:

my_date    | nice_dates 
03-05-2020 | 03/05/2020
04-05-2020 | 04/05/2020
05-05-2020 | 05/05/2020
06-05-2020 | 06/05/2020

I have tried using the lubridate pacakge but get stuck when defining the new format i know it should be "%d/%m/%Y" however using as.Date() yields NA.

Should i just use gsub instead?


Solution

  • @ThoVu is almost there. So for completeness, a much simpler answer with only base R.

    Code
    ## input data as before
    df <- data.frame(my_date = c("03-05-2020", "04-05-2020", 
                                 "05-05-2020", "06-05-2020"))
    
    ## create Date objects using base R
    df$parsed <- strptime(df$my_date, "%d-%m-%Y")
    
    ## format them to spec
    df$nice_dates <- format(df$parsed, "%d/%m/%Y")
    
    Output
    R> df <- data.frame(my_date = c("03-05-2020", "04-05-2020",
                                    "05-05-2020", "06-05-2020"))
    R> df$parsed <- strptime(df$my_date, "%d-%m-%Y")
    R> df$nice_dates <- format(df$parsed, "%d/%m/%Y")
    R> df
         my_date     parsed nice_dates
    1 03-05-2020 2020-05-03 03/05/2020
    2 04-05-2020 2020-05-04 04/05/2020
    3 05-05-2020 2020-05-05 05/05/2020
    4 06-05-2020 2020-05-06 06/05/2020
    R> 
    

    My general recommendation is to never ever use string manipulations or regular expressions on date inputs when you could use proper date parsers.