Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R gsub multiple conditions

Tags:

r

gsub

I am trying to use gsub on a set of strings that might all vary in the wording slightly;

I went to the store last night
I went to the park yesterday
I went to starbucks this morning

I need to use gsub to replace the 'I went to...', but sometimes it will have a 'the' and sometimes it won't

Something like this, but the following won't work properly

gsub('i went to [the|a-z]','REPLACED',string)

REPLACED last night
REPLACED yesterday
REPLACED this morning
like image 855
chattrat423 Avatar asked Jun 10 '26 08:06

chattrat423


1 Answers

Try:

gsub("I went to (the )?[a-z]", "REPLACED", string)
like image 197
user2280549 Avatar answered Jun 13 '26 22:06

user2280549



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!