[R] converting string fractions to numerics

Rolf Turner rolf.turner at xtra.co.nz
Wed Oct 19 09:06:54 CEST 2011


On 19/10/11 19:30, Erin Hodgess wrote:
> Dear R People:
>
> Suppose I have the following:
>
> "pi/2"
>
> and I would like it to be 1.57.....
>
> Using as.numeric, here is my result:
>
>> as.numeric("pi/2")
> [1] NA
> Warning message:
> NAs introduced by coercion
>>
>
> Is there a way to produce the numeric result, please?

Same answer: parse --- despite what fortune() says.

s <- "pi/2"
eval(parse(text=s))
[1] 1.570796

	cheers,

		Rolf



More information about the R-help mailing list