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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Mike Lonergan wrote:> > Why is as.numeric("3/2") NA plus a warning?Because "3/2" is a character string that cannot be coerced to numeric easily. You mean: You want to evaluate the expression(!) 3/2, which is - whyever - inquotation marks. Maybe the following line is more appropriate to you problem? eval(parse(text="3/2")) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 24 Jul 2002, Mike Lonergan wrote:> > Why is as.numeric("3/2") NA plus a warning?Because that is not a valid number (it's a text representation of an expression). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._