34

I was using a function that requires input as integers.

So I have been trying to read up on making things integers:

y <- 3.14
as.integer(y)
[1] 3              # all cool

All good, but if I have

 x <- 1639195531833
 as.integer(x)
 [1] NA
 Warning message:
 NAs introduced by coercion 

I had options(digits = 15) on and it confused my why it wasn't working but in a clean session it must be to do with the scientific notation.

I also tried to trick R but it was not happy:

  as.integer(as.character(x))
[1] 2147483647
Warning message:
inaccurate integer conversion in coercion 

How do I defeat scientific notation and get my integers?

4
  • 2
    See ?as.integer, which tells you integers are restricted to approximately +/-2*10^9. Jan 29, 2013 at 18:08
  • 1
    forget it. Integers in R are still 32bit, so you can't have integers larger than about 1.2E9. This will probably change in the next major R version, if rumours are right.
    – Joris Meys
    Jan 29, 2013 at 18:08
  • ouch. This requires a lot of renaming. thanks guys, shall I delete this Q or would you like an answer here? Jan 29, 2013 at 18:10
  • 2
    it's a valid question with now a valid answer, so leave it be I'd say
    – Joris Meys
    Jan 29, 2013 at 18:13

4 Answers 4

55

The largest integer R can hold is

.Machine$integer.max
# [1] 2147483647

This has nothing to do with scientific notation and everything to do with how the computer actually stores the numbers. The current version of R stores integers still as 32bit, regardless of the architecture. This might change in the future though.

see also ?as.integer

Currently you can get access to 64 bit integers through the int64 package

> as.integer(.Machine$integer.max)
[1] 2147483647
> # We get problems with this
> as.integer(.Machine$integer.max + 1)
[1] NA
Warning message:
NAs introduced by coercion 
> # But if we use int64
> library(int64)
> as.int64(.Machine$integer.max) + 1L
[1] 2147483648
0
14

Update to Dason's answer (don't have enough reputation to make a comment):

The int64 package has now been deprecated, however there is now a package called bit64. I was able to achieve the same result using it. The syntax changed only slightly from "as.int64" to "as.integer64".

I.e.

library(bit64)
as.integer64(.Machine$integer.max) + 1L
4

seems int64 is already deprecated. I use package bit64, method as.integer64() and solve the problem. details are referred to here.

2

There are classes that can handle large integers. I use int64, its vignette is here:

http://cran.r-project.org/web/packages/int64/vignettes/int64.pdf

To use this you just place some normal number into that class then you can add or multiply it across the threshold of what the normal 32bit integer max. Good luck.

1
  • +1 this looks interesting thank you, although getting that class into the function im running might be tough but good to know. Jan 29, 2013 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.