Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading