[R] Conversion from expression to numeric

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 23 18:20:17 CET 2006


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

> Dear All
> 
> I am trying to convert from the type "expression" to the type
> "numeric". The following works:
> 
> > x <- expression(6.2)
> > as.numeric(as.character(x))
> [1] 6.2
> 
> However, the following does not work:
> 
> > x <- expression(62/100)
> > as.numeric(as.character(x))
> [1] NA
> Warning message:
> NAs introduced by coercion
> 
> Any idea about how to deal with the second case?

Well, you could start by watching where you are going...

> x <- expression(62/100)
> x
expression(62/100)
> as.character(x)
[1] "62/100"

and as.numeric wouldn't know dashes about strings that contain
slashes...

eval(x) might be closer to the mark.

-- 
   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