Home > Back-end >  Truncate String after ";" in R?
Truncate String after ";" in R?

Time:10-04

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!

CodePudding user response:

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.

  • Related