Search code examples
rdatecharacter

How to convert a character column with a specific date format into date without having some values turn into NAs?


My dataframe looks like this:

Date_1 (char)   Date_2 (char)
Jul 10, 2019    Jul 13, 2019
Sep 10, 2018    Dec 14, 2018
Oct 25, 2021    Nov 14, 2022
...

I have not found a fitting code yet to convert these two character columns into type date. Output should be something like 'mm/dd/yyyy'. Grateful for any tips.

EDIT: Here is my dput

structure(list(Start_Date = c("Dec 12, 2017", "Oct 19, 2022","Oct 3, 2022"), End_Date = c("Dec 25, 2017", "Dec 19, 2022", "Dec 1, 2022"), Completion = c("100% completed", "34% completed", "62% completed")), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"))

Maels answer works for some values but others are just turned into NAs - see dput in comment section.


Solution

  • There are two steps.

    1. First convert to a date object. You can use as.Date and you have to specify how the date is formatted in the first place.
    2. Then you can format it as you like. You should use the format function.

    This might a bit confusing since format is used twice for different purposes.

    Date_1 = "Jul 10, 2019"
    as.Date(Date_1, format = "%b %d, %Y") |>
      format("%m-%d-%Y")
    #[1] "07-10-2019"
    

    Edit, in base R, using your dput:

    data <- structure(list(Start_Date = c("Dec 12, 2017", "Oct 19, 2022","Oct 3, 2022"), End_Date = c("Dec 25, 2017", "Dec 19, 2022",  "Dec 1, 2022"), Completion = c("100% completed", "34% completed",  "62% completed")), row.names = c(NA, -3L), class = c("tbl_df",  "tbl", "data.frame"))
    
    data[c("Start_Date", "End_Date")] <- 
      sapply(data[c("Start_Date", "End_Date")],
             function(x) as.Date(x, format = "%b %d, %Y") |>
               format("%m-%d-%Y"))