[R] as.numeric

Mike Lonergan mel at mcs.st-and.ac.uk
Wed Jul 24 19:22:23 CEST 2002


Thanks folks,

I apologise for my imprecision. I think I do understand what is going on
with as.numeric("3/2"), but am less clear as to the reasons for it to have
been chosen.

If I may rephrase my question:

as.numeric("3") returns 3 and as.numeric(3/2) gives 1.5. Would it be very
stupid of me to modify as.numeric to make it also evaluate text strings that
do not contain any variable names ( as.numeric("3/2") would then give 1.5
and as.numeric("2+3") 5 but as.numeric("p") would remain NA) ?

Or is there already a function that has this effect? I guess it would be
safer to make a new function rather than overwriting, but do find that when
testing things I seem to end up using improper fractions. (I was wanting to
collect a set of parameters with match.call(), but using eval(parse()) to
deal with my fractions ended up also attempting to evaluate the characters
of non-numeric parameters - eg type="p"; eval(parse(text=type)) looks for
the value of a variable called p. Maybe it would just be easier to learn to
divide.)

Thanks again for the advice,

Mike.


     > -----Original Message-----
     > From: Yuelin Li [mailto:yuelin at pandora.outcomes.chop.edu]
     > Sent: 24 July 2002 15:18
     > To: mel at mcs.st-and.ac.uk
     > Subject: Re: [R] as.numeric
     >
     >
     > Try
     >
     >  eval(parse(text="3/2"))
     >
     > because "3/2" is not converted to a mathematical expression
     > unless parsed.
     >
     > Yuelin.
     >
     > ---------
     >
     >  Why is as.numeric("3/2") NA plus a warning?
     >
     > Thanks,
     >
     > Mike.
     >

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list