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.
There are two steps.
as.Date
and you have to specify how the date is formatted in the first place.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"))