我想用对应的 ascii 替换非 ascii 字符(目前只有西班牙语)。如果我有“á”,我想用“a”替换它,依此类推。
我构建了这个函数(工作正常),但我不想使用循环(包括像 sapply 这样的内部循环)。
latin2ascii<-function(x) {
if(!is.character(x)) stop ("input must be a character object")
require(stringr)
mapL<-c("á","é","í","ó","ú","Á","É","Í","Ó","Ú","ñ","Ñ","ü","Ü")
mapA<-c("a","e","i","o","u","A","E","I","O","U","n","N","u","U")
for(y in 1:length(mapL)) {
x<-str_replace_all(x,mapL[y],mapA[y])
}
x
}
有没有优雅的方法来解决它?任何帮助、建议或修改表示赞赏