Re: [R] converting string fractions to numerics

2011-10-19 Thread Erin Hodgess
Here is another possibility: > xxx [1] "pi/2" "1/2" > sapply(strsplit(xxx,split="/"), + function(x) { + x <- ifelse(x=="pi",pi,x) + as.numeric(x[1])/as.numeric(x[2]) + } + ) [1] 1.570796 0.50 > Thanks for everyone's help! Sincerely, Erin On Wed, Oct 19, 2011 at 2:06 AM, Rolf Turner wrote:

Re: [R] converting string fractions to numerics

2011-10-19 Thread Rolf Turner
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

Re: [R] converting string fractions to numerics

2011-10-18 Thread baptiste auguie
Hi, I believe you want eval(parse(text="pi/2")) a word of warning exemplified in eval(parse(text="library(fortunes) ; fortune(106)")) HTH, baptiste On 19 October 2011 19:30, Erin Hodgess wrote: > Dear R People: > > Suppose I have the following: > > "pi/2" > > and I would like it to be 1.5

Re: [R] converting string fractions to numerics

2011-10-18 Thread R. Michael Weylandt
I would imagine that you could parse+evaluate it like you asked about on another thread; this isn't tested though. Does that work in your context? Michael Weylandt On Oct 19, 2011, at 2:30 AM, Erin Hodgess wrote: > Dear R People: > > Suppose I have the following: > > "pi/2" > > and I would

[R] converting string fractions to numerics

2011-10-18 Thread Erin Hodgess
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? Thanks, Erin -- Erin Hodgess Associa