34

我正在尝试替换两个“st”。和“ste”。与“圣”。似乎以下应该工作,但它没有:

require("stringr")
county <- c("st. landry", "ste. geneveve", "st. louis")
str_replace_all(county, c("st\\.", "ste\\."), "st")
4

2 回答 2

65

你可以|用来表示“或”

> str_replace_all(county, "st\\.|ste\\.", "st")
[1] "st landry"   "st geneveve" "st louis"   

或者在基础 R

> gsub("st\\.|ste\\.", "st", county)
[1] "st landry"   "st geneveve" "st louis"  
于 2012-09-07T15:02:01.557 回答
1
> A<-"this string,  contains a handful of,  useless:  punctuation.  Some are to escape.  Aaargh! Some might be needed,  but I want none!"
> gsub(", |: |\\. |!","",A)
[1] "this string contains a handful of useless punctuation Some are to escape Aaargh Some might be needed but I want none"
于 2019-03-04T17:15:29.127 回答