R 与整数斗争(最大整数大小)

R 与整数斗争(最大整数大小),r,R,我使用的函数要求输入为整数 因此,我一直在努力: 我如何打败科学记数法并得到我的整数?R能容纳的最大整数是 .Machine$integer.max # [1] 2147483647 这与科学记数法无关,而与计算机实际存储数字的方式有关。当前版本的R将整数存储为32位,而与体系结构无关。不过,这在未来可能会改变 另请参见?as.integer 目前,您可以通过int64包访问64位整数 > as.integer(.Machine$integer.max) [1] 2147483647 &

我使用的函数要求输入为整数

因此,我一直在努力:


我如何打败科学记数法并得到我的整数?

R能容纳的最大整数是

.Machine$integer.max
# [1] 2147483647
这与科学记数法无关,而与计算机实际存储数字的方式有关。当前版本的R将整数存储为32位,而与体系结构无关。不过,这在未来可能会改变

另请参见
?as.integer

目前,您可以通过int64包访问64位整数

> 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

有些类可以处理大整数。我使用int64,其亮点如下:


要使用它,您只需将一些普通数字放入该类中,然后就可以在普通32位整数最大值的阈值范围内将其相加或相乘。祝您好运。

似乎int64已经被弃用了。我使用包bit64,方法
作为.integer64()
解决了这个问题。详情请参考。

更新Dason的答案(没有足够的声誉发表评论):

int64包现在已被弃用,但是现在有一个名为bit64的包。我用它也能达到同样的效果。语法从“as.int64”到“as.integer64”只有轻微的变化


请参见
?as.integer
,它告诉您整数被限制为大约+/-2*10^9。忘记它吧。R中的整数仍然是32位的,所以不能有大于1.2E9的整数。如果传言属实,这可能会在下一个主要的R版本中发生变化。哎哟。这需要大量重命名。谢谢大家,我要删除这个问题,还是你想在这里得到答案?这是一个有效的问题,现在有了一个有效的答案,所以让它保持不变,我会说+1这看起来很有趣,谢谢你们,虽然让这个类进入我运行的函数可能很难,但很高兴知道。
  as.integer(as.character(x))
[1] 2147483647
Warning message:
inaccurate integer conversion in coercion 
.Machine$integer.max
# [1] 2147483647
> 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
library(bit64)
as.integer64(.Machine$integer.max) + 1L