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

Extracting year from date time variable

I have some date/times in a variable where I would only like to extract the year.

Here is an example:

2/10/21 6:55

I would only like to extract ‘2021’ from this.

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

By the way, the class for this variable is ‘character’.

> class(df$DTS)
[1] "character"

>Solution :

library(lubridate)
"2/10/21 6:55" |> mdy_hm() |> year()
# [1] 2021
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