Skip to content

How do I get 10^4 to become 10000?

6 messages · Marc Schwartz, Patrick Connolly, Peter Dalgaard +1 more

#
Of course, that's as trivial as it gets on the command line, but I
can't work out how to get a column of numbers that are entered as
"10^5" from its 'character' format into a numeric one?

I feel a bit embarrassed asking such a simple question.  Too much
Easter....

Thanks
#
Connolly
For an individual value you can use:
[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


HTH,

Marc Schwartz
#
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
1 day later
#
How about this?
[1] 1.0e+05 1.2e+03 2.3e+00

This solution doesn't work for "10^(-5)" but does work for "10^-5".

-Don
At 1:51 PM +1200 4/22/03, Patrick Connolly wrote:

  
    
#
Don MacQueen <macq at llnl.gov> writes:
Didn't work too well for 1.2^3 by some standards...
[1] 10000.000     1.728     2.300
#
Oops.
Red face here.
-Don
At 5:42 PM +0200 4/23/03, Peter Dalgaard BSA wrote: