简体   繁体   中英

How do I convert to numeric an exponential stored as a char in R?

I have a file with numbers in scientific notation stored as 0.00684*10^0.0023. When I read the file with read.csv(), they are loaded as character strings, and trying to convert them with the as.numeric() function returns only NA.

a = "0.00684*10^0.0023"

as.numeric(a)

NA

Is there a way to force R to take on the variable as Scientific notation?

Here are a few ways. They use a from the question (also shown in the Note below) or c(a, a) to illustrate how to apply it to a vector. No packages are used.

1) Use eval/parse:

eval(parse(text = a))
## [1] 0.00687632

or for a vector such as c(a, a)

sapply(c(a, a), function(x) eval(parse(text = x)), USE.NAMES = FALSE)
## [1] 0.00687632 0.00687632

2) A different way is to split up the input and calculate it ourself.

with(read.table(text = sub("\\*10\\^", " ", c(a, a))), V1 * 10^V2)
## [1] 0.00687632 0.00687632

3) A third way is to convert to complex numbers and the combine the real and imaginary parts to get the result.

tmp <- type.convert(sub("\\*10\\^(.*)", "+\\1i", c(a, a)), as.is = TRUE)
Re(tmp) * 10^Im(tmp)
## [1] 0.00687632 0.00687632

4) Another way to split it up is:

as.numeric(sub("\\*.*", "", c(a, a))) * 
  10^as.numeric(sub(".*\\^", "", c(a, a)))
## [1] 0.00687632 0.00687632

6) We could use any of the above to define a custom class which can read in fields of the required form. First we write out a test data file and then define a custom num class. In read.csv use the colClasses argument to specify which class each column has. Use NA for those columns where we want read.csv to determine the class automatically..

# generate test file
cat("a,b\n1,0.00684*10^0.0023\n", file = "tmpfile.csv")

setAs("character", "num",
 function(from) with(read.table(text = sub("\\*10\\^", " ", from)), V1 * 10^V2))

read.csv("tmpfile.csv", colClasses = c(NA, "num"))
##   a          b
## 1 1 0.00687632

With this definition we can also use as like this:

as(c(a, a), "num")
## [1] 0.00687632 0.00687632

Note

a <- "0.00684*10^0.0023"

One idea:

library(stringr)
a <- "0.00684*10^0.0023" # your input
b <- str_split(a, "\\*10\\^") # split the string by the operator
b <- as.numeric(b[[1]]) # transform the individual elements to numbers

c <- b[1]*10^(b[2]) # execute the wished operation with the obtained numbers
c # the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM