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

Extract Month from partial date in R

I have a partial date string

partial <- "UN-Jan-2022"

My desired output

> parital
  Jan

I’m trying to do this by using gsub, I can remove "UN-" or -"2022" but not sure how to remove both in one gsub

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

gsub("UN-", "", partial)
Jan-2022
gsub("-2022", "", partial)
UN-Jan

>Solution :

Another gsub option which extracts everything between both "-" like this:

partial <- "UN-Jan-2022"
gsub(".*-(.*)\\-.*", "\\1", partial)
#> [1] "Jan"

Created on 2022-07-27 by the reprex package (v2.0.1)

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