5

I have a BigInt number.If I try to store it in R

  R> a <- 9223372036854775807
  R> a
   [1] 9.223372e+18

As you can notice its loosing info of last few digits. I tried multiple other ways to solve this but no luck like increasing options(digits = 22) or changing to numeric, double, integer.

 > as.integer(9223372036854775807)
  [1] NA
  Warning message:
  NAs introduced by coercion to integer range 
 R> as.numeric(9223372036854775807)
  [1] 9.223372e+18
 R> as.double(9223372036854775807)
  [1] 9.223372e+18

Can anyone help me with this problem. I want to retain the same original value. I also do not want to install any external package.

1 Answers1

4

We can use as.integer64 from bit64

library(bit64)
as.integer64(as.character(a))
#integer64
#[1] 9223372036854775807
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Unfortunately, I can't install external package as mentioned in question. – shivank agrawal Apr 04 '18 at 04:48
  • @shivankagrawal Then, probably you may have to adjust with the reduced precision, Even if you try with `as.character`, it will reduce the accuracy i.e. `as.character(a)# [1] "9223372036854775808"` Check the last digit it is rounded – akrun Apr 04 '18 at 04:48