简体   繁体   中英

Error in replacing text in R

I m replacing one text in R using sub.

 x<-"My name is ${name}"
 sub("${name}","Tiger",x)

Error Message:

Error in sub("${name}", "Tiger", x) : 
  invalid regular expression '${name}', reason 'Invalid contents of {}'

Input text has {}, How to fix this error?

$ , { , and } need to be escaped:

sub("\\$\\{name\\}","Tiger",x)
# [1] "My name is Tiger"

Use the fixed=TRUE argument:

sub("${name}","Tiger",x, fixed=TRUE)
# [1] "My name is Tiger"

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