문제

I am trying to replace both "st." and "ste." with "st". Seems like the following should work but it does not:

require("stringr")
county <- c("st. landry", "ste. geneveve", "st. louis")
str_replace_all(county, c("st\\.", "ste\\."), "st")
도움이 되었습니까?

해결책

You can use | to mean "or"

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

Or in base R

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

다른 팁

> 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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top