'R Class turning to NULL instead of date
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 1:[1]
You can Try below Solution**
data <- structure(
list(
LICENCE_RENEWAL_DATE = c(" ", " ", " ", "2022-02-12T07:42:49Z", "2022-02-10T13:52:37Z",
" ", " ", "2022-02-13T16:37:48Z", " ", " "),
LICENCE_RENEWAL_DATE2 = c(" ", " ", " ", "2022-02-12", "2022-02-10", " ", " ",
"2022-02-13", " ", " ")),
row.names = c(NA, 10L),class = "data.frame")
Don't involve NA while creating Data
full_data <- data %>%
mutate(LICENCE_RENEWAL_DATE2 = substr(LICENCE_RENEWAL_DATE, 1, 10)) %>%
select(LICENCE_RENEWAL_DATE, LICENCE_RENEWAL_DATE2)
Convert Both Columns in DateTime ("POSIXct" "POSIXt") and Date
library(anytime)
full_data$LICENCE_RENEWAL_DATE <- anytime(full_data$LICENCE_RENEWAL_DATE)
full_data$LICENCE_RENEWAL_DATE2 <- anydate(full_data$LICENCE_RENEWAL_DATE2)
Check Data Types
> class(full_data$LICENCE_RENEWAL_DATE)
[1] "POSIXct" "POSIXt"
> class(full_data$LICENCE_RENEWAL_DATE2)
[1] "Date"
I Hope this solution will resolved your issue
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 | user229044 |
