I have the following string:
a = "in the year 2010-01-01 there was a red dog and in the year 2011-01-01 there was a blue cat"
My Problem: In the above string, I want to replace every 2010 with 2011 and every 2011 with 2012.
I tried the following code:
b = gsub("2010", "2011", a)
b = gsub("2011", "2012", b)
But it overwrites my progress!
I did this with 3 mini statements:
b = gsub("2010", "temp", a)
b = gsub("2011", "2012", b)
b = gsub("temp", "2011", b)
But is there a quicker way to do this in a single line of code?
If I understand correctly, you can simply replace 2011 to 2012 first, followed by 2010 to 2011, in that order:
More generally, you could use a regex replacement with a callback function.