OneBite.Dev - Coding blog in a bite size

replace a word in a string in R

Code snippet on how to replace a word in a string in R

gsub("word1", "word2", x)

This code replaces a word, “word1”, with a new word, “word2”, in the string, “x”. The “gsub” command looks through the string, “x”, and searches for any occurrence of the word, “word1”, which it then replaces with “word2”. So, after running the command, any occurrences of “word1” will be replaced with “word2” in the string “x”.

r