在较长的脚本中,我必须将向量 A 的长度 (2614) 乘以数据帧 B 的行数 (1456000)。如果我直接这样做length(A) * nrow(B)
我收到消息NAs produced by integer overflow
尽管当我乘以相同的数字时没有问题:
2614 * 1456000
[1] 3805984000
让乘法起作用的唯一方法是round(length(A)) * nrow(B)
or length(A) * round(nrow(B))
。但产生的数字length
and nrow
无论如何必须是整数!此外,我使用函数 is.integer 的帮助页面上建议的以下函数对此进行了测试...
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x-round(x)) < tol
...当然,它们是整数。那么为什么我需要“圆形”拐杖呢?非常令人费解...有人知道后台发生了什么吗?
希望有一个图形表示正在发生的事情......
2614 * 1456000
#[1] 3805984000
## Integers are actually represented as doubles
class( 2614 * 1456000 )
#[1] "numeric"
# Force numbers to be integers
2614L * 1456000L
#[1] NA
#Warning message:
#In 2614L * 1456000L : NAs produced by integer overflow
## And the result is an integer with overflow warning
class( 2614L * 1456000L )
#[1] "integer"
#Warning message:
#In 2614L * 1456000L : NAs produced by integer overflow
2614 * 1456000
is a numeric
因为两个操作数实际上都是同一类numeric
。发生溢出是因为nrow
and length
return integer
's,因此结果是一个整数,但结果超出了可表示的最大大小integer
类(+/-2*10^9)。 Anumeric
or double
能把持住2e-308 to 2e+308
。所以要解决你的问题,只需使用as.numeric(length(A))
or as.double(length(A))
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)