[R] How do I get 10^4 to become 10000?

Patrick Connolly p.connolly at hortresearch.co.nz
Tue Apr 22 05:54:07 CEST 2003


On Mon, 21-Apr-2003 at 10:23PM -0500, Marc Schwartz wrote:


|> For an individual value you can use:
|> 
|> > eval(parse(text = "10 ^ 5"))
|> [1] 1e+05
|> 
|> 
|> However, that simple approach does not work with a vector, since the
|> 'text' argument is treated as if it were single lines in an input
|> file.  Thus you need to do something like this to loop through the
|> vector elements:
|> 
|> # create the character vector
|> cv <- c("10 ^ 5", "10 ^ 4", "10 ^ 8")
|> 
|> # function to convert char vector elements to numeric
|> convtext <- function(x) eval(parse(text = x))
|> 
|> # use sapply to convert the vector
|> sapply(cv, convtext, USE.NAMES = FALSE)
|> [1] 1e+05 1e+04 1e+08

That's precisely what I needed to know.  Thanks

Thanks also to Tony Plate, Ray Brownrigg, and Robin Hankin for
reminding me of the 'text' argument to parse().

best

-- 
Patrick Connolly
HortResearch
Mt Albert
Auckland
New Zealand 
Ph: +64-9 815 4200 x 7188
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it.  ---Steven Wright 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~



More information about the R-help mailing list