[R] Conversion from expression to numeric

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 23 19:04:15 CET 2006


"Paul Smith" <phhs80 at gmail.com> writes:

> Thanks, Peter. I did not know about the existence of the command
> 'eval', in spite of the fact of having searched the Internet for a
> solution for my problem.
> 
> 'eval' seems not working in connection with Ryacas:
> 
> > library(Ryacas)
> > x <- yacas("2/3*5/7")
> > x
> expression(10/21)
> > eval(x)
> expression(10/21)
> >
> 
> To work, one has to do the following:
> > eval(x)
> expression(10/21)
> > eval(as.expression(x))
> [1] 0.4761905

This could be the following effect in action:

> quote(expression(10/21))
expression(10/21)
> eval(quote(expression(10/21)))
expression(10/21)
> eval(eval(quote(expression(10/21))))
[1] 0.4761905

which is one of the weirder anomalies in the parse/deparse area in the
R language. Part of the secret is

> quote(expression(10/21))
expression(10/21)
> mode(quote(expression(10/21)))
[1] "call"
> mode(expression(10/21))
[1] "expression"

and you might look at mode(x) to see whether this is relevant to Ryacas.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list