Remove accents

Asked

Viewed 9,711 times

17

I need to know how to remove the accents of a die in a column.

# Eu tentei
> library(stringr)
> a <- dados$Municipio[2]
> a
[1] "Arapeí"
> str_replace_all(a, "[í]", "i")
[1] "Arapeí"


# outra tentativa
> iconv(a, to="ASCII//TRANSLIT")
[1] NA

Can someone help me?

7 answers

15

A while ago I set up this function to remove accents. Never let me down.

rm_accent <- function(str,pattern="all") {
  # Rotinas e funções úteis V 1.0
  # rm.accent - REMOVE ACENTOS DE PALAVRAS
  # Função que tira todos os acentos e pontuações de um vetor de strings.
  # Parâmetros:
  # str - vetor de strings que terão seus acentos retirados.
  # patterns - vetor de strings com um ou mais elementos indicando quais acentos deverão ser retirados.
  #            Para indicar quais acentos deverão ser retirados, um vetor com os símbolos deverão ser passados.
  #            Exemplo: pattern = c("´", "^") retirará os acentos agudos e circunflexos apenas.
  #            Outras palavras aceitas: "all" (retira todos os acentos, que são "´", "`", "^", "~", "¨", "ç")
  if(!is.character(str))
    str <- as.character(str)

  pattern <- unique(pattern)

  if(any(pattern=="Ç"))
    pattern[pattern=="Ç"] <- "ç"

  symbols <- c(
    acute = "áéíóúÁÉÍÓÚýÝ",
    grave = "àèìòùÀÈÌÒÙ",
    circunflex = "âêîôûÂÊÎÔÛ",
    tilde = "ãõÃÕñÑ",
    umlaut = "äëïöüÄËÏÖÜÿ",
    cedil = "çÇ"
  )

  nudeSymbols <- c(
    acute = "aeiouAEIOUyY",
    grave = "aeiouAEIOU",
    circunflex = "aeiouAEIOU",
    tilde = "aoAOnN",
    umlaut = "aeiouAEIOUy",
    cedil = "cC"
  )

  accentTypes <- c("´","`","^","~","¨","ç")

  if(any(c("all","al","a","todos","t","to","tod","todo")%in%pattern)) # opcao retirar todos
    return(chartr(paste(symbols, collapse=""), paste(nudeSymbols, collapse=""), str))

  for(i in which(accentTypes%in%pattern))
    str <- chartr(symbols[i],nudeSymbols[i], str)

  return(str)
}

9

Use this function:

fa <- function(x) iconv(x, to = "ASCII//TRANSLIT")

fa(c("pelé","época"))

[1] "pele"  "epoca"

4

Function stri_trans_general package stringi

stri_trans_general("Arapeí", "Latin-ASCII")

3

Have you tried using the function gsub?

The use is: gsub(padrao a ser substituido, padrao usado na substituicao, string)

For example:

coluna = c("aaaí","eeeeí","ooooí")

gsub("í", "i", coluna)

[1] "aaai"  "eeeei" "ooooi"

3

I like it this way:

s <- c("ájakla","ééhasj", "hsíklf", "fdhjó")
chartr("áéíó", "aeio", s)
[1] "ajakla" "eehasj" "hsiklf" "fdhjo" 

This way just add the accented characters and their substitutions in order.

0

Some time ago I developed this solution for my PHP environment,

function changeLetters($string, $down = true){

        $letters = array(
            'A'=>array('@','â','ä','à','å','Ä','Å','á','ª','Á','Â','À','ã','Ã'),
            'E'=>array('&','é','ê','ë','è','É','£','Ê','Ë','È'),
            'I'=>array('!','ï','î','ì','¡','Í','Î','Ï','Ì','í'),
            'O'=>array('ô','ö','ò','Ö','ø','Ø','ó','º','¤','ð','Ó','Ô','Ò','õ','Õ'),
            'U'=>array('ü','û','ù','Ü','ú','µ','Ú','Û','Ù'),
            'B'=>array('ß'),
            'C'=>array('Ç','ç','©','¢'),
            'D'=>array('Ð'),
            'F'=>array('ƒ'),
            'L'=>array('¦'),
            'N'=>array('ñ','Ñ'),
            'S'=>array('$','§'),
            'X'=>array('×'),
            'Y'=>array('ÿ','¥','ý','Ý'),
            'AE'=>array('æ','Æ'),
            'P'=>array('þ','Þ'),
            'R'=>array('®'),
            '0'=>array('°'),
            '1'=>array('¹','ı'),
            '2'=>array('²'),
            '3'=>array('³'),
        );

        foreach ($letters as $letter => $change){
            if($down){ $letter = down($letter); }
            $string = str_replace($change, $letter, $string);
        }

        return $string;
    }

I know your question is based on language r, but posted more as help for possible variant of letters.

-1

I found this code which is very simple and worked.

x = chartr("ÁÉÍÓÚ", "AEIOU", x)
  • 1

    But what about the other accents?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.