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

Transform number chain into date

I am trying to transform a list of numbers (e.g. 20200119) into a valid date (here: 2020-01-19)

This is my trial data:

df <- data.frame(c(20200119, 20180718, 20180729, 20150502, 20010301))
colnames(df)[1] = "Dates"

enter image description here

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

And this is what I tried so far:

df <- as_date(df)
df <- as.Date.numeric(df)
df <- as.Date.factor(df)

Neither of them works unfortunately.
I also tried to seperate the numbers, but I couldn’t achieve either.

Can somebody help me?

>Solution :

Convert it to a character and convert it then to a Date with given format %Y%m%d:

as.Date(as.character(df$Dates), "%Y%m%d")
#[1] "2020-01-19" "2018-07-18" "2018-07-29" "2015-05-02" "2001-03-01"
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