2012-01-22 530 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()'函數的幾個用法好得多,以''''替換每個標點符號。 –

相關問題