'How to remove spanish accents but keep the "ñ"

I'm triying to remove accents in a string, but keeping the letter ñ.

"DIVISIÓN DE MONTAÑA" |> stringi::stri_trans_general("Latin-ASCII") does the job of removing all accents and the letter ñ, resulting in DIVISION DE MONTANA.

Is there other transformation available in stringi, that keeps the letter ñ?



Solution 1:[1]

x <- "DIVISIÓN DE MONTAÑA"

paste(sapply(strsplit(x, "")[[1]], function(x) ifelse(x %in% c("Ñ", "ñ"), x, stringi::stri_trans_general(x, "Latin-ASCII"))), collapse = "")

# [1] "DIVISION DE MONTAÑA"

Solution 2:[2]

One way to circumvent it is to replace the ñ with a character you don't need before the transliteration and replace them back afterwards like so:

"DIVISIÓN DE MONTAÑñA" |> 
  stringi::stri_replace_all_regex(pattern = c("ñ", "Ñ"), replacement = c("¬", "¤"), vectorize=F) |> 
  stringi::stri_trans_general("Latin-ASCII") |> 
  stringi::stri_replace_all_regex(pattern = c("¬", "¤"), replacement = c("ñ", "Ñ"), vectorize=F)

It's not the prettiest solution tho. And you have to make sure the symbols are not being transliterated themselves. The merit however is that the process is very quick and it will save time compared to a character by character strsplit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Merijn van Tilborg
Solution 2 Wietse de Vries