Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 R(中)使用 sub 替换?
(
)
让定义x为:
x
x="abc(def"
然后当我尝试用(其他东西替换时,会发生错误:
sub("(","",x)
错误是:
'Missing ')''
正如 Kohske 所说,您需要双重转义,但您也可以使用参数fixed=TRUE:
fixed=TRUE
sub("\\(","",x) sub("(","",x,fixed=TRUE)
两者都给你:
[1] "abcdef"
你需要逃避:
> sub("\\(", "@", x) [1] "abc@def"