【问题标题】:Removing certain characters from a string in R从R中的字符串中删除某些字符
【发布时间】:2013-02-16 16:38:03
【问题描述】:

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

>docs

....

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

....

所以我想知道如何从字符串中删除这些 \u009 字符(所有这些字符,其中一些数字略有不同)。我试过使用gsub(),但这在从字符串中删除内容方面没有效果。

【问题讨论】:

    标签: string r


    【解决方案1】:

    这应该可以工作

    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"
    

    【讨论】:

    • 关于“您必须始终指定 4 位数字”:这仅在执行 unicode 时。这应该可以很好地删除空格和破折号:gsub(' |-', '', ' 1-444-654 ')
    • 我在想,不是有类似Excel的right()或left()的函数吗?
    【解决方案2】:

    尝试: gsub('\\$', '', '$5.00$')

    【讨论】:

    • 尽可能简单但不简单。完美!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多