user3710832
user3710832

Reputation: 415

Why does gsub not work as expected

I have this string:

c <- "thethirsty thirsty itthirsty (thirsty) is"

I want the output to be as

     "thethirsty thirsty itthirsty no is"

This is what I am trying.

gsub(" (thirsty) ", " no ", c)

This is what I am getting. Why does not it work? And suggest an alternative to do this.

"thethirsty no itthirsty (thirsty) is" 

Upvotes: 0

Views: 380

Answers (1)

Roland
Roland

Reputation: 132969

By default gsub interprets the first parameter as a regular expression. You don't want that and should set fixed=TRUE:

gsub(" (thirsty) ", " no ", c, fixed=TRUE)
#[1] "thethirsty thirsty itthirsty no is"

Upvotes: 1

Related Questions