为什么as.integer在R中返回NA?

xyy*_*xyy 3 memory integer r

我注意到如果我有一个大于10位的数字,该as.integer函数将返回NA.

例如:

as.integer(10000000000)
Run Code Online (Sandbox Code Playgroud)

会给NA.

为什么会这样?我想这可能与整数的存储有关?我该如何解决这个问题?

谢谢.

DGK*_*son 9

您可以通过以下方式找到整数限制:

> .Machine$integer.max
[1] 2147483647
Run Code Online (Sandbox Code Playgroud)

任何更大的值都将被解释为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 
Run Code Online (Sandbox Code Playgroud)

如果你需要处理更大的值,可以使用as.numeric(数值可以处理比整数更大的值)或像gmp(多精度算术包)这样的包.