I have been using the mapvalues
function in R to convert pre-defined character strings into their desired form.
However some of these character strings are not unique, and what I would like to convert them to depends on a criteria in another column.
For example take the following dataframe:
df <- data.frame(Name = c("Audrey", "Belinda", "Caroline", "Caroline" "Dina", "Erica"),
Country = c("China", "Germany", "England", "America", "India", "America"))
I would like to convert the American Caroline to 'Caz' using another data frame (where the desired form is specified):
dfmap <- data.frame(Name = c("Audrey", "Belinda", "Caroline", "Caroline", "Dina", "Erica"),
Country = c("China", "Germany", "England", "America", "India", "America"),
NameCorrect = c("Audrey", "Belinda", "Caroline", "Caz", "Dina", "Erica"),
CountryCorrect = c("China", "Germany", "England", "America", "India", "America"))
I can't simply use the mapvalues
function on the Name
and NameCorrect
columns as it wont be able to differentiate the English Caroline from the American Caroline.
How can I get R to differentiate between the American Caroline and English Caroline, and then map the values to the desired output stored in the dfmap
dataframe?