So I have data as follows
data <- structure(list(LICENCE_RENEWAL_DATE = c("NA", "NA", "NA", "2022-02-12T07:42:49Z",
"2022-02-10T13:52:37Z", "NA", "NA", "2022-02-13T16:37:48Z", "NA",
"NA"), LICENCE_RENEWAL_DATE2 = c("NA", "NA", "NA", "2022-02-12",
"2022-02-10", "NA", "NA", "2022-02-13", "NA", "NA")), row.names = c(NA,
10L), class = "data.frame")
I get this by running the following:
data <- full_data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
I thought I could just sandwich the substr() with as_date() from lubridate but this is not working. The class for LICENCE_RENEWAL_DATE2 is NULL and I dont know what to make of that. But I would need this to be class = Date.
>Solution :
You can do something like this:
full_data$LICENCE_RENEWAL_DATE2 <- as.Date(full_data$LICENCE_RENEWAL_DATE2, format = "%Y-%m-%d")
It will convert the LICENCE_RENEWAL_DATE2 column’s class to Date.
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"