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? Thanks in advance, Paul
Paul Smith wrote:>>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? >eval-uate the expression: > x <- expression(62/100) > eval(x) [1] 0.62 Barry
On 11/23/06, Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> wrote:> >>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? > > > > eval-uate the expression: > > > x <- expression(62/100) > > eval(x) > [1] 0.62That is it, Barry. Thanks. Paul
"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) > xexpression(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