R 如何删除字符串中非常特殊的字符?
R how to remove VERY special characters in strings?
我正在尝试删除字符串中的一些非常特殊的字符。
我读过其他 post 比如:
- Remove all special characters from a string in R?
- How to remove special characters from a string?
但这不是我要找的。
假设我的字符串如下:
s = "who are í ½í¸€ bringing?"
我试过以下方法:
test = tm_map(s, function(x) iconv(enc2utf8(x), sub = "byte"))
test = iconv(s, 'UTF-8', 'ASCII')
上面的 none 有效。
编辑:
我正在寻找通用解决方案!
我不能(也不愿意)手动识别所有特殊字符。
这些非常特殊的字符也可能(不是 100% 肯定)是表情符号的结果
请帮助或引导我找到正确的 post。
谢谢!
所以,我要继续做一个答案,因为我相信这就是你要找的:
> s = "who are í ½í¸€ bringing?"
> rmSpec <- "í|½|€" # The "|" designates a logical OR in regular expressions.
> s.rem <- gsub(rmSpec, "", s) # gsub replace any matches in remSpec and replace them with "".
> s.rem
[1] "who are ¸ bringing?"
现在,需要注意的是您必须在 rmSpec
变量中手动定义特殊字符。不确定您是否知道要删除哪些特殊字符,或者您是否正在寻找更通用的解决方案。
编辑:
所以看起来您几乎已经用 iconv
了,您只是缺少 sub
参数。见下文:
> s
[1] "who are í ½í¸€ bringing?"
> s2 <- iconv(s, "UTF-8", "ASCII", sub = "")
> s2
[1] "who are bringing?"
我正在尝试删除字符串中的一些非常特殊的字符。 我读过其他 post 比如:
- Remove all special characters from a string in R?
- How to remove special characters from a string?
但这不是我要找的。
假设我的字符串如下:
s = "who are í ½í¸€ bringing?"
我试过以下方法:
test = tm_map(s, function(x) iconv(enc2utf8(x), sub = "byte"))
test = iconv(s, 'UTF-8', 'ASCII')
上面的 none 有效。
编辑: 我正在寻找通用解决方案! 我不能(也不愿意)手动识别所有特殊字符。
这些非常特殊的字符也可能(不是 100% 肯定)是表情符号的结果
请帮助或引导我找到正确的 post。 谢谢!
所以,我要继续做一个答案,因为我相信这就是你要找的:
> s = "who are í ½í¸€ bringing?"
> rmSpec <- "í|½|€" # The "|" designates a logical OR in regular expressions.
> s.rem <- gsub(rmSpec, "", s) # gsub replace any matches in remSpec and replace them with "".
> s.rem
[1] "who are ¸ bringing?"
现在,需要注意的是您必须在 rmSpec
变量中手动定义特殊字符。不确定您是否知道要删除哪些特殊字符,或者您是否正在寻找更通用的解决方案。
编辑:
所以看起来您几乎已经用 iconv
了,您只是缺少 sub
参数。见下文:
> s
[1] "who are í ½í¸€ bringing?"
> s2 <- iconv(s, "UTF-8", "ASCII", sub = "")
> s2
[1] "who are bringing?"