Replace accented characters in R with non-accented counterpart (UTF-8 encoding) [duplicate]
The below answers are basically taken from elsewhere. The key is getting your unwanted_array
in the right format. You might want it as a list
:
unwanted_array = list( 'Š'='S', 'š'='s', 'Ž'='Z', 'ž'='z', 'À'='A', 'Á'='A', 'Â'='A', 'Ã'='A', 'Ä'='A', 'Å'='A', 'Æ'='A', 'Ç'='C', 'È'='E', 'É'='E', 'Ê'='E', 'Ë'='E', 'Ì'='I', 'Í'='I', 'Î'='I', 'Ï'='I', 'Ñ'='N', 'Ò'='O', 'Ó'='O', 'Ô'='O', 'Õ'='O', 'Ö'='O', 'Ø'='O', 'Ù'='U', 'Ú'='U', 'Û'='U', 'Ü'='U', 'Ý'='Y', 'Þ'='B', 'ß'='Ss', 'à'='a', 'á'='a', 'â'='a', 'ã'='a', 'ä'='a', 'å'='a', 'æ'='a', 'ç'='c', 'è'='e', 'é'='e', 'ê'='e', 'ë'='e', 'ì'='i', 'í'='i', 'î'='i', 'ï'='i', 'ð'='o', 'ñ'='n', 'ò'='o', 'ó'='o', 'ô'='o', 'õ'='o', 'ö'='o', 'ø'='o', 'ù'='u', 'ú'='u', 'û'='u', 'ý'='y', 'ý'='y', 'þ'='b', 'ÿ'='y' )
You can do this easily with iconv
or chartr
:
> iconv(string, to='ASCII//TRANSLIT')[1] "Holmer"> chartr(paste(names(unwanted_array), collapse=''), paste(unwanted_array, collapse=''), string)[1] "Holmer"
Otherwise you have to loop through all of replacements because mapply
or similar wouldn't account for symbols already replaced by previous gsub
operations.:
# the loop:out <- stringfor(i in seq_along(unwanted_array)) out <- gsub(names(unwanted_array)[i],unwanted_array[i],out)
The result:
> out[1] "Holmer"
Another option is to use gsubfn
package:
library(gsubfn)string="Hølmer"gsubfn(paste(names(unwanted_array),collapse='|'), unwanted_array,string)[1] "Holmer"