Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is as.integer returning NA in R?

Tags:

integer

memory

r

I have noticed that if I have a number larger than 10 digits, the as.integer function will return NA.

For example:

as.integer(10000000000)

will give NA.

Why does this happen? I guess it might have something to do with the storage of integers? How can I work around this issue?

Thanks.

like image 936
xyy Avatar asked Oct 31 '25 08:10

xyy


1 Answers

You can find the limit for integers by:

> .Machine$integer.max
[1] 2147483647

Any bigger values will be interpreted as NA

> as.integer(.Machine$integer.max)
[1] 2147483647
> as.integer(.Machine$integer.max+1)
[1] NA
Warning message:
NAs introduced by coercion to integer range 

If you need to handle bigger values, either use as.numeric (numerics can handle bigger values than integers) or a package like gmp (Multiple Precision Arithmetic package).

like image 142
DGKarlsson Avatar answered Nov 03 '25 00:11

DGKarlsson