3

I have this value in character:

 val <- "1e-14105"

What I want to do is to convert them into numeric and preserve the scientific notation. So the result is simply 1e-14105 as number.

I tried this but failed:

> as.numeric(val)
[1] 0
> format(as.numeric(val), scientific=TRUE)
[1] "0e+00"

What's the right way to do it?

littleworth
  • 4,781
  • 6
  • 42
  • 76

1 Answers1

3

The problem is a floating point issue and the degree of accuracy to which R is working. It is converting 1e-14105 to numeric, it's just then approximating it to zero. See The R Inferno: Circle 1, Falling into the Floating Point Trap.

> as.numeric("1e-14105")
[1] 0
> class(as.numeric("1e-14105"))
[1] "numeric"
> 1e-14105
[1] 0
Scransom
  • 3,175
  • 3
  • 31
  • 51
  • 1
    So there's no way to keep `1e-14105` as it is in numeric scientific format? – littleworth Oct 20 '17 at 03:40
  • 2
    @qqq - maybe consider some of the packages for dealing with very small/very large numbers mentioned in here - https://stackoverflow.com/questions/5802592/dealing-with-very-small-numbers-in-r – thelatemail Oct 20 '17 at 04:08