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

Strip off string parts by a pattern R

I have a variable called summed_EU_Sales, I want to get EU only.

I have tried to do it using gsub this way:

gsub(".*summed_", "", summed_EU_Sales)

which only removes the first summed_. I struggle to remove both of the sides.

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 :

Assuming your variable name summed_EU_Sales is actually a string "summed_EU_Sales", we can use regex groupings:

sub(".*_(EU)_.*", "\\1", "summed_EU_Sales")
#> [1] "EU"

Created on 2022-12-19 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