I\'m looking for a way to use the find and replace function in R to replace the entire value of a string, rather than just the matching part of the string. I have a dataset with
gsub() is used to substitute a particular string with another string. In the above code, if you do the following, your whole string changes to exp
result <- gsub(string, "exp", string)
But, if you use grep() and replace(), you will achieve your desired result.
res1 <- grep("pattern",string)
gives you all the lines with the pattern and use this in replace().
res_new <- replace(string,res1,"exp")