I have two strings:
mystring1 <- c("hello i am a cat. just kidding, i'm not a cat i'm a cat. dogs are the best animal. not cats!")
mystring2 <- c("hello i am a cat. just kidding, i'm not a cat i'm a cat. but i have a cat friend that is a cat.")
I want to change the third occurrence of the word cat in both strings to dog.
Ideally, string1 and string2 would read:
mystring1
[1] "hello i am a cat. just kidding, i'm not a cat i'm a dog. dogs are the best animal. not cats!"
mystring2
[1] "hello i am a cat. just kidding, i'm not a cat i'm a dog. but i have a cat friend that is a cat."
What is the best way of doing this? Up until now I have only used gsub to replace characters but I don't know if this can be used to replace specific occurrences of a character.