Find and Replace inside specific elements of data frames


Hi all,

I have a file which contains plant species in Australia and I have found a spelling error. What I want to do is a simple find and replace of all instances of “perinniana” with “perriniana” as you would be able to easily do in excel, without having to create versioning issues by importing back and forth.

I looked up functions which are capable of doing this and found “gsub” but don’t understand it’s output or how to use it properly.

Any advice is much appreciated!



I think I fixed it! You can’t seem to apply the function to a whole dataframe, but it works quite fine if you work element by element and make sure there is an appropriate assignment.



Hi Katherine. If the instances take up the whole data cell, you can do this:

data$species[data$species == "perinniana"] <- "perriniana"

If the instances are parts of a string, then gsub is exactly right, something like this:

species <- c("Acropora perinniana", "Acropora perinniana", "Acropora perinniana", "Acropora perriniana")
gsub("perinniana", "perriniana", species)

where species is the data.frame column where you find the bad name. You can assign the change directly back to the data.frame column, e.g.:

species <- gsub("perinniana", "perriniana", species)