Replace several character variables

Hi, i have the next data frame
str(PobCuenca)
'data.frame': 1825 obs. of 19 variables:
X : int 1 2 3 4 5 6 7 8 9 10 ... CveUn : num 12.2 12.4 12.4 12.6 12.7 ...
Ambito : chr "Urbano" "Rural" "Rural" "Rural" ... CveMun : int 12 12 12 12 12 12 148 148 148 211 ...
$ Municipio : chr "Candelaria Loxicha" "Candelaria Loxicha" "Candelaria Loxicha" "Candelaria Loxicha" ...

In the variable $ Municpio i have the town name. Neverless some of them are misspleiings and I need replace several of them
list(unique(PobCuenca$Municipio))
[[1]]
[1] "Candelaria Loxicha" "San Francisco Ozolotepec" "San Juan Ozolotepec"
[4] "San Marcial Ozolotepec" "San Mateo Piñas" "San Mateo Río Hondo"
[7] "San Miguel del Puerto" "San Miguel Suchixtepec" "San Pedro el Alto"
[10] "San Pedro Mixtepec -Dto. 26 -" "San Pedro Pochutla" "San Sebastián Río Hondo"
[13] "Santa María Huatulco" "Santa María Ozolotepec" "Santa María Tonameca"
[16] "Santiago Xanica" "Santo Domingo Ozolotepec" "Pluma Hidalgo"
[19] "San Agustín Loxicha" "San Mateo Piñas" "San Mateo Río Hondo"
[22] "San Pedro Mixtepec" "San Sebastián Río Hondo" "Santa María Huatulco"
[25] "Santa María Ozolotepec" "Santa María Tonameca" "San Agustín Loxicha"

I tried with replace and gsub but i coldn`t do it. How can i replace all of them for the correct? For example "San Agustín Loxicha" = "San Agustin Loxicha"

Thanks

You could use case_when()

1 Like

Another way is to use a named vector as a lookup table. Something like this:

# vector with "wrong spelling" = "right spelling" pairs
corrected <- c(
  "San Francisco Ozolotepec" = "San Juan Ozolotepec",
  "San Marcial Ozolotepec" = "San Juan Ozolotepec",
 ...
)

library(dplyr)

# apply the correction
PobCuenca %>%
  mutate(
    Municipio = if_else(Municipio %in% names(corrected),
                        corrected[Municipio],
                        Municipio)
  )
···
You can also use two columns of a dataframe for this.
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.