2016-05-16 5 views
0

いくつかの名前のリストがありますが、そのうちのいくつかはスペルが間違っています。stri_replace_all_regexを使用したスペルミスの名前の除外

fruits <- c("apple", "two pears", "three bananas", "appl3") 

私は「A」で始まり、文字列「りんご」でそれを置き換える任意の文字列を検索するstringiパッケージの下stri_replace_all_regexメソッドを使用したいと思います。

しかし、正規表現を使用しても、達成しようとしていることがわかりません。

私は立ち往生しています

stri_replace_all_regex(fruits,"^a","apple") 
[1] "applepple"  "two pears"  "three bananas" "appleppl3" 

stri_replace_all_regex(fruits,"^a(pple)?$","apple") 
[1] "apple"   "two pears"  "three bananas" "appl3" 
+0

たとえば、「two appl3s」ではどうなりますか? –

答えて

1

あなたのソリューションが「A」で始まる文字列を検索し、「A」、代わりに文字列「りんご」を追加しますので、 ppleがリンゴなること取り除き pple。

stri_replace_all_regex(fruits, "^a.*", "apple") 
+0

ありがとう、ちょうど私が探していた答え。 –

0

あなたはsubまたはgsub

baseを使用して、それをうまくすることができます:あなたが欲しい

で始まる文字列全体を選択し、新しい文字列「りんご」でそれを置換することです

fruits <- c("apple", "two pears", "three bananas", "appl3") 
gsub("^a.*", "apple", fruits) 

"apple"   "two pears"  "three bananas" "apple" 
+0

ありがとう、私の例では 'gsub()'と 'stri_replace_all_regex'の両方が同じことを成し遂げてくれたことは間違いありません。 –

関連する問題