我想替换:
(1)", "
(逗号+空格)带"_"
(下划线)
(2)"'"
(撇号)与"'s"
(撇号+ s)
library(gsubfn) x <- c("Mary' car is red.", "A, B, C") gsubfn(".", list(", " = "_", "'" = "'s"), x)
我想是"Mary's car is red."
和"A_B_C"
,但结果却是"Mary's car is red."
和"A, B, C"
.为什么?
试试这个:
toreplace<-list(", " = "_", "'" = "'s") gsubfn(paste(names(toreplace),collapse="|"),toreplace,x) #[1] "Mary's car is red." "A_B_C"
你的方法的问题是你的模式只是一个单一的字符(.
)并且无法匹配", "
.