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

Changing the format of pairs of years in a vector in R

I was wondering if it might be possible to change the format of my Input to my Desired_output?

Input= c("20192020","20202021","20212022")

Desired_output= c("2019-20","2020-21","2021-22")

>Solution :

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

We can use sub() here:

Input <- c("20192020", "20202021", "20212022")
out <- sub("\\d{2}(\\d{2})$", "-\\1", Input)
out

[1] "2019-20" "2020-21" "2021-22"
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