Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 714
Manish Avatar asked Jan 25 '13 04:01

Manish


2 Answers

Use the fixed=TRUE argument:

sub("${name}","Tiger",x, fixed=TRUE)
# [1] "My name is Tiger"
like image 65
sebastian-c Avatar answered Nov 02 '22 07:11

sebastian-c


$, {, and } need to be escaped:

sub("\\$\\{name\\}","Tiger",x)
# [1] "My name is Tiger"
like image 37
flodel Avatar answered Nov 02 '22 07:11

flodel