Decoding character strings vectors in R

Viewed 313

Suppose you have an atomic vector containing encoded character strings:

string_encoding <- c("São Paulo", "Paraná")

Is there any way to decode every element in the vector, returning a vector of the same length with ASCII, ISO-8859-1 or other class of encoding?

The output should be:

expected_encoding <- c("Sao Paulo", "Parana")

2 Answers

Using stringi, you can do:

stri_trans_general(string_encoding, "Latin-ASCII")

[1] "Sao Paulo" "Parana"

Another option can be:

iconv(string_encoding, to='ASCII//TRANSLIT')

Output:

[1] "Sao Paulo" "Parana"
Related