我有一个字符串,我想删除所有非字母数字符号,然后放入一个矢量.所以这:
"This is a string. In addition, this is a string!"
Run Code Online (Sandbox Code Playgroud)
会成为:
>stringVector1
"This","is","a","string","In","addition","this","is","a","string"
Run Code Online (Sandbox Code Playgroud)
我看过grep()但找不到匹配的例子.有什么建议?
koh*_*ske 33
这是一个例子:
> 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"
Run Code Online (Sandbox Code Playgroud)
小智 5
处理这个问题的另一种方法
library(stringr)
text = c("This is a string. In addition, this is a string!")
str_split(str_squish((str_replace_all(text, regex("\\W+"), " "))), " ")
#[1] "This" "is" "a" "string" "In" "addition" "this" "is" "a" "string"
Run Code Online (Sandbox Code Playgroud)
str_replace_all(text, regex("\\W+"), " "):查找非单词字符并替换" "str_squish():减少字符串内重复的空格str_split():将字符串分成几部分