我使用的函数要求输入为整数。
所以我一直在尝试read up on making things integers:
y <- 3.14
as.integer(y)
[1] 3 # all cool
一切都很好,但是如果我有
x <- 1639195531833
as.integer(x)
[1] NA
Warning message:
NAs introduced by coercion
我打开了
options(digits = 15)
,它使我感到困惑,为什么它不起作用,但是在干净的 session 中,这一定与科学记数法有关。我也尝试欺骗R,但并不高兴:
as.integer(as.character(x))
[1] 2147483647
Warning message:
inaccurate integer conversion in coercion
如何克服科学计数法并获得整数?
最佳答案
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
关于r - 在整数中挣扎(最大整数大小),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14589354/