'How to convert the proper class in R
I tried to convert the class from character to date it showing an error Error in charToDate(x) : character string is not in a standard unambiguous format
dput(my_data$exit_date)
c("0000-00-00", "2019-08-25", "2019-12-31", "2020-04-02", "2020-09-15",
"0000-00-00", "0000-00-00", "0000-00-00", "2019-12-08", "0000-00-00",
"2020-11-30", "2020-09-01", "0000-00-00", "2020-02-15", "2020-07-24",
"2021-03-22", "2020-07-03", "2020-03-26", "2020-02-24", "2019-05-15",
"0000-00-00", "2020-01-28", "0000-00-00", "0000-00-00", "0000-00-00",
"0000-00-00", "0000-00-00", "0000-00-00", "2021-03-22", "2021-03-25",
"2021-03-22", "0000-00-00", "0000-00-00", "0000-00-00", "0000-00-00",
"0000-00-00")
my_data$exit_date <- as.Date(my_data$exit_date)
Solution 1:[1]
You need to specify the format of the date. Error is probably because it can't guess from the initial date of 0s.
d1 <- c("0000-00-00", "2019-08-25", "2019-12-31", "2020-04-02", "2020-09-15",
"0000-00-00", "0000-00-00", "0000-00-00", "2019-12-08", "0000-00-00",
"2020-11-30", "2020-09-01", "0000-00-00", "2020-02-15", "2020-07-24",
"2021-03-22", "2020-07-03", "2020-03-26", "2020-02-24", "2019-05-15",
"0000-00-00", "2020-01-28", "0000-00-00", "0000-00-00", "0000-00-00",
"0000-00-00", "0000-00-00", "0000-00-00", "2021-03-22", "2021-03-25",
"2021-03-22", "0000-00-00", "0000-00-00", "0000-00-00", "0000-00-00",
"0000-00-00")
as.Date(d1, format = "%Y-%m-%d")
Seeing as you tagged this a lubridate (which I'd always use anyway) here's how you would do it in that.
lubridate::ymd(d1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quixotic22 |
