简体   繁体   中英

In R, how to use regex [:punct:] in gsub?

Given

test<-"Low-Decarie, Etienne"

I wish to replace all punctuation with space

gsub(pattern="[:punct:]", x=test, replacement=" ")

but this produces

"Low-De arie, E ie  e"

where no punctuation is replaced and apparently random letters are removed (though they may be associated with punctation as t for tab and n for next line).

Fellow MontReal user here.

Several options, sames results.

In R Base, just double the brackets

gsub(pattern="[[:punct:]]", test, replacement=" ")

[1] "Low Decarie  Etienne"

Package stringr has function str_replace_all that does that.

library(stringr)
str_replace_all(test, "[[:punct:]]", " ")

Or keep only letters

str_replace_all(test, "[^[:alnum:]]", " ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM