[R] translating a character string

Rolf Turner rolf.turner at xtra.co.nz
Wed Oct 19 03:27:01 CEST 2011


On 19/10/11 13:57, Erin Hodgess wrote:
> Dear R People:
>
> Suppose I have the following character string:
>
>
>> f1
> [1] "(1/30)*(20-x)"
>
> My goal is to end up with
> y<- (1/30)*(20-x)
>
> How would I do this, please?
>
> I've been experimenting with eval, but no good so far.
>
> As usual, I have the feeling that this is something really simple, but
> I can't quite get it.
>
> Thank you in advance for any help.

Irrespective of fortune("parse"), parse() is what you need:

f1 <- "(1/30)*(20-x)"
x <- 42
y <- eval(parse(text=f1))
y
[1] -0.7333333

     cheers,

         Rolf



More information about the R-help mailing list