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?
CodePudding user response:
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
Note
a <- "0.00684*10^0.0023"
CodePudding user response:
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