2012-01-22 623 views
11

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

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

将成为:

>stringVector1 

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

我看了grep(),但无法找到匹配的例子。有什么建议么?

回答

26

这里有一个例子:

> 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" 
+0

我注意到有在结束方括号之间的正则表达式的空间。那是什么用的? –

+1

@ B.Mr.W。它保留字符串中的空格在 – mlegge

+1

由于分裂,最后,我毫不避讳地使用正则表达式中的R'GSUB(“[^ [:alnum:] = \\]”,“”,“哦,等等等等等等,只是安静!= 0.42“)比累积'gsub()'函数的几个用法好得多,以''''替换每个标点符号。 –

相关问题