Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

r - struggling with integers (maximum integer size)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...