Replace multiple letters with accents with gsub
Use the character translation function
chartr("áéó", "aeo", mydata)
An interesting question! I think the simplest option is to devise a special function, something like a "multi" gsub():
mgsub <- function(pattern, replacement, x, ...) {
if (length(pattern)!=length(replacement)) {
stop("pattern and replacement do not have the same length.")
}
result <- x
for (i in 1:length(pattern)) {
result <- gsub(pattern[i], replacement[i], result, ...)
}
result
}
Which gives me:
> mydata <- c("á","é","ó")
> mgsub(c("á","é","ó"), c("a","e","o"), mydata)
[1] "a" "e" "o"
Maybe this can be usefull:
iconv('áéóÁÉÓçã', to="ASCII//TRANSLIT")
[1] "aeoAEOca"
You can use stringi
package to replace these characters.
> stri_trans_general(c("á","é","ó"), "latin-ascii")
[1] "a" "e" "o"
This is very similar to @kith, but in function form, and with the most common diacritcs cases:
removeDiscritics <- function(string) {
chartr(
"ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ"
,"SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy"
, string
)
}
removeDiscritics("test áéíóú")
"test aeiou"