我正在进行一些计算并收到以下警告(即不是错误):
Warning messages:
1: In sum(myvar, na.rm = T) :
Integer overflow - use sum(as.numeric(.))
在这个 thread人们说整数溢出根本不会发生。要么 R 不是太现代,要么它们是不正确的。但是,我应该在这里做什么?如果我使用
as.numeric
正如警告所暗示的那样,我可能无法解释以前信息丢失的事实。 myvar
是从 .csv 文件中读取的,所以 R 不应该弄清楚需要一些更大的字段吗?它是否已经切断了某些东西?integer
的最大长度是多少?或 numeric
?您会建议任何其他字段类型/模式吗?编辑:我跑:
R 版本 2.13.2 (2011-09-30)
平台:R Studio 中的 x86_64-apple-darwin9.8.0/x86_64(64 位)
最佳答案
简而言之,integer
是范围有限的精确类型,numeric
是一种浮点类型,可以表示更广泛的值但不精确。有关详细信息,请参阅帮助页面(?integer
和 ?numeric
)。
至于溢出,这里是 explanation布赖恩·D·里普利(Brian D. Ripley):
It means that you are taking the mean [in your case, the sum -- @aix] of some very large integers, and the calculation is overflowing. It is just a warning.
This will not happen in the next release of R.
您可以通过给它后缀
L
来指定一个数字是一个整数。 ,例如 1L
是整数 1,而不是 1
这是一个浮点数,类 "numeric"
.您可以在机器上创建的最大整数由
.Machine$integer.max
给出。 .> .Machine$integer.max
[1] 2147483647
> class(.Machine$integer.max)
[1] "integer"
添加一个正整数会导致溢出,返回
NA
.> .Machine$integer.max + 1L
[1] NA
Warning message:
In .Machine$integer.max + 1L : NAs produced by integer overflow
> class(.Machine$integer.max + 1L)
[1] "integer"
您可以通过添加浮点值来绕过此限制。
> .Machine$integer.max + 1
[1] 2147483648
> class(.Machine$integer.max + 1)
[1] "numeric"
因为在您的情况下,警告是由
sum
发出的。 ,这表明当数字相加时发生溢出。建议的解决方法 sum(as.numeric(.))
应该做的伎俩。
关于r - 什么是 R 中的整数溢出,它是如何发生的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804779/