Search and replace

Search and replace of text is easily done with gsub

# load iris data set
data(iris)

# search and replace 'Width' with 'nothing' in the names of the iris data

gsub("Width", "nothing", names(iris))
## [1] "Sepal.Length"  "Sepal.nothing" "Petal.Length"  "Petal.nothing"
## [5] "Species"

gsub("width", "nothing", names(iris), ignore.case = TRUE)
## [1] "Sepal.Length"  "Sepal.nothing" "Petal.Length"  "Petal.nothing"
## [5] "Species"

# in case you run into trouble with some data, use 'as.character' to convert
# the vector to characters. Not needed here, just fyi.
gsub("setosa", "nothing", as.character(iris$Species))
##   [1] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##   [6] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [11] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [16] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [21] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [26] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [31] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [36] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [41] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [46] "nothing"    "nothing"    "nothing"    "nothing"    "nothing"   
##  [51] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [56] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [61] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [66] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [71] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [76] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [81] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [86] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [91] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
##  [96] "versicolor" "versicolor" "versicolor" "versicolor" "versicolor"
## [101] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [106] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [111] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [116] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [121] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [126] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [131] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [136] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [141] "virginica"  "virginica"  "virginica"  "virginica"  "virginica" 
## [146] "virginica"  "virginica"  "virginica"  "virginica"  "virginica"

Other text-search tools

# read ?grep, etc

# 'grep' returns only the inidces of the matching elements
grep("Width", names(iris))
## [1] 2 4

# 'grepl' return true/false for each element in the vector
grepl("Width", names(iris))
## [1] FALSE  TRUE FALSE  TRUE FALSE

# create an example vector
x <- c("invisible", "iteration")
x
## [1] "invisible" "iteration"

# 'gsub', as seen above, returns both the changed and unchanged elements of
# the vector. It works 'general', i.e. for all found matches
gsub("i", "X", x)
## [1] "XnvXsXble" "XteratXon"

# 'sub' only works on the first element that matches the search pattern! It
# doe not work 'general' as 'gsub (see above)!
sub("i", "X", x)
## [1] "Xnvisible" "Xteration"
sub("t", "X", x)
## [1] "invisible" "iXeration"
# some characters with reserved characters, i.e. characters that have more
# meaning.  from ?regex: The metacharacters in EREs are . \ | ( ) [ { ^ $ *
# + ?, but note that whether these have a special meaning depends on the
# context.

x <- c(".", "*", "[")
x
## [1] "." "*" "["

# to get rid of special 'powers' that a character has, use double
# backslashes
gsub(".", "Dot", x)
## [1] "Dot" "Dot" "Dot"
# '.' means 'itself', in this case referring to the replacement, matches any
# pattern!

# Use '\\' to get rid of this special meaning to just replace the
# character
gsub("\\.", "Dot", x)
## [1] "Dot" "*"   "["
gsub("[", "Bracket", x)
## Error: invalid regular expression '[', reason 'Missing ']''
gsub("\\[", "Bracket", x)
## [1] "."       "*"       "Bracket"
gsub("*", "Start", x)
## [1] "Start.Start" "Start*Start" "Start[Start"
gsub("\\*", "Start", x)
## [1] "."     "Start" "["
# '*' means repetition

so, be careful with your replacement patterns