[R] How do I get 10^4 to become 10000?
mschwartz at medanalytics.com
Tue Apr 22 05:23:08 CEST 2003
>From: r-help-bounces at stat.math.ethz.ch
>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Patrick
>Sent: Monday, April 21, 2003 8:51 PM
>Subject: [R] How do I get 10^4 to become 10000?
>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
For an individual value you can use:
> eval(parse(text = "10 ^ 5"))
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
# 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)
 1e+05 1e+04 1e+08
More information about the R-help