4

スペースに変換したい句読点を含む文字列がたくさんあります。

"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パッケージ ( ) 句読点記号を一度に 1 つずつ (, / . / ! / ( / ) / など)手動で実行することもできますが、str_replace_all()正規表現を使用すると想定するより高速な方法があるかどうか知りたいです.

助言がありますか?

4

2 に答える 2

12
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:
      ‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { |
      } ~’.
于 2012-07-16T04:39:44.547 に答える
4

見て?regex

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

"This is a string  In addition  this is a string  with one more  "
于 2012-07-16T04:39:13.943 に答える