gsubfn()
in a package with the same name is really nice for this kind of thing:
library(gsubfn) # Create a named list, in which: # - the names are the strings to be looked up # - the values are the replacement strings mapL <- c("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ","ü","Ü") mapA <- c("a","e","i","o","u","A","E","I","O","U","n","N","u","U") # ll <- setNames(as.list(mapA), mapL) # An alternative to the 2 lines below ll <- as.list(mapA) names(ll) <- mapL # Try it out string <- "ÍÓáÚ" gsubfn("[áéíóúÁÉÍÓÚñÑüÜ]", ll, string) # [1] "IOaU"
Edit:
G. Grothendieck indicates that the base R also has a function for this:
A <- paste(mapA, collapse="") L <- paste(mapL, collapse="") chartr(L, A, "ÍÓáÚ")