Reputation: 1105
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.
Upvotes: 0
Views: 420
Reputation: 120
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
Upvotes: 0
Reputation: 1381
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"
Upvotes: 1