在R中从字符串中删除特定字符

42

我在R中有一个包含大量单词的字符串。当查看该字符串时,会得到大量文本,其中包括类似以下文本:

>docs

....

\u009cYes yes for ever for ever the boys cried in their ringing voices with softened faces

....

我在想如何从字符串中删除这些\u009字符(其中一些具有略微不同的数字)。我尝试使用gsub(),但它无法有效地从字符串中删除内容。

2个回答

58

这应该可以工作

gsub('\u009c','','\u009cYes yes for ever for ever the boys ')
"Yes yes for ever for ever the boys "

这里的009c是Unicode的十六进制数,你必须始终指定4个十六进制数字。 如果你有很多,一个解决方案是用管道符号分隔它们:

gsub('\u009c|\u00F0','','\u009cYes yes \u00F0for ever for ever the boys and the girls')

"Yes yes for ever for ever the boys and the girls"

1
关于“必须始终指定4位数”的问题:这仅适用于Unicode。这应该可以很好地去除空格和破折号: gsub(' |-', '', ' 1-444-654 ') - Zak
我在想,是否有类似于Excel的right()或left()函数? - Eric Lino

12

try: gsub('\\$', '', '$5.00$')


尽可能简单,但不要过于简单。完美! - Cris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接