首页 文章

R从字符串中删除非字母数字符号

提问于
浏览
13

我有一个字符串,我想删除所有非字母数字符号,然后放入一个矢量 . 所以这:

"This is a string.  In addition, this is a string!"

会成为:

>stringVector1

"This","is","a","string","In","addition","this","is","a","string"

我看了 grep() 但找不到匹配的例子 . 有什么建议?

1 回答

  • 29

    这是一个例子:

    > str <- "This is a string. In addition, this is a string!"
    > str
    [1] "This is a string. In addition, this is a string!"
    > strsplit(gsub("[^[:alnum:] ]", "", str), " +")[[1]]
     [1] "This"     "is"       "a"        "string"   "In"       "addition" "this"     "is"       "a"       
    [10] "string"
    

相关问题