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

Truncate String after ";" in R?

I have some cells with long strings.
I want to truncate the cells within a column, so that only word(s) before a semicolon are maintained. For example, if I have a cell with the string blue house; with green garden I want to only maintain the words before the semicolon, so it would become blue house

Thank you!

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 :

blue <- c('blue house; with green garden')
gsub('(;*?);.*', '\\1', blue)
[1] "blue house"

appears to work. When using the very nice regex101.com, and you get something to work, remember to add another \ to working examples when you use them in your terminal or RStudio.

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