2012-07-16 80 views
2

我有一堆與他們標點符號的字符串,我想轉換爲空格:轉換標點符號空間

"This is a string. In addition, this is a string (with one more)." 

將成爲:

"This is a string In addition this is a string with one more " 

我可以走通,做這個手工用stringr包(str_replace_all())一次一個標點符號(,/。/!/(/)/等),但我很好奇,如果有更快的方式,我會假設使用正則表達式的。

有什麼建議嗎?

回答

9
x <- "This is a string. In addition, this is a string (with one more)." 
gsub("[[:punct:]]", " ", x) 
[1] "This is a string In addition this is a string with one more " 

做快速換人喜歡這個顯示?gsub,並?regex有關[[:punct:]]類的細節,即

‘[:punct:]’ Punctuation characters: 
     ‘! " # $ % & ' () * + , - ./: ; <=> ? @ [ \ ]^_ ` { | 
     } ~’. 
4

看看?regex

library(stringr) 
str_replace_all(x, '[[:punct:]]',' ') 

"This is a string In addition this is a string with one more " 
+2

這不是一個基地R函數,所以你需要添加一個對'stringr'的引用。 – Andrie 2012-07-16 05:37:11

+0

我正在使用OP已經聲明他們使用'stringr'和'str_replace_all'的事實。感謝@Josh的相關編輯。 – mnel 2012-07-16 06:41:41