簡體   English   中英

big64 - NA矢量上的sum()產生奇數結果

[英]big64 - sum() on a vector of NA produces odd results

當使用big64包,求和的矢量NAs為整數的另一矢量產生不准確的結果。 根據NA向量是首先求和還是最后求和,結果將分別是正確答案的0或兩倍。

請注意,將NA向量轉換為integer64將消除該問題。

然而,當試驗其他小值代替y時,結果非常奇怪。 例如:

40 + 35 = 75    but
35 + 40 = 80

有什么想法發生了什么?

例:

  library(bit64)

  x <- as.integer64(c(20, 20))
  y <- as.integer64(c(NA, NA))

  sum(y, x, na.rm=TRUE)
  # integer64
  # [1] 80   # <~~~ Twice the correct value

  sum(x, y, na.rm=TRUE)
  # integer64
  # [1] 0   # <~~~~ Incorrect 0.  Should be 40. 

  ## Removing the NAs does not help. 
  y <- y[!is.na(y)]

  ## A vector of 0's gives the same issue
  y <- as.integer64(c(0, 0))

  ## Same results
  sum(y, x, na.rm=TRUE)
  # integer64
  # [1] 80

  sum(x, y, na.rm=TRUE)
  # integer64
  # [1] 0

  ## Converting to numeric does away with the issue (but is not a viable workaround, for obvious reasons)
  y <- as.numeric(y)

  sum(y, x, na.rm=TRUE)
  # [1] 1.97626e-322

  sum.integer64(y, x, na.rm=TRUE)
  # integer64
  # [1] 40

  sum(x, y, na.rm=TRUE)
  # integer64
  # [1] 40

y一個值,結果也非常不合適

  y <- as.integer64(c(35, NA, NA))
  sum.integer64(x, if (!all(is.na(y))) removeNA(y), na.rm=TRUE)
  sum.integer64(x, y[[1]], na.rm=TRUE)
  sum.integer64(y[[1]], x, na.rm=TRUE)

  ## No NA's present
  sum.integer64(as.integer64(35), x)
  # integer64
  # [1] 80
  sum.integer64(x, as.integer64(35))
  # integer64
  # [1] 70

不是答案,而是探索。 希望它可以幫到你。

bit64包的sum.integer64函數:

function (..., na.rm = FALSE) 
{
    l <- list(...)
    ret <- double(1)
    if (length(l) == 1) {
        .Call("sum_integer64", l[[1]], na.rm, ret)
        oldClass(ret) <- "integer64"
        ret
    }
    else {
        ret <- sapply(l, function(e) {
            if (is.integer64(e)) {
                .Call("sum_integer64", e, na.rm, ret)
                ret
            }
            else {
                as.integer64(sum(e, na.rm = na.rm))
            }
        })
        oldClass(ret) <- "integer64"
        sum(ret, na.rm = na.rm)
    }
}

這是你的例子:

library(bit64)
x <- as.integer64(c(20, 20))
y <- as.integer64(c(NA, NA))

na.rm <- TRUE
l <- list(y, x)
ret <- double(1)
ret
#[1] 0

# We use the sapply function as in the function:
ret <- sapply(l, function(e) { .Call("sum_integer64", e, na.rm, ret) })
oldClass(ret) <- "integer64"
ret
#integer64
#[1] 40 40      <-- twice the value "40"
sum(ret, na.rm = na.rm)
# integer64
#[1] 80         <-- twice the expected value, as you said

在這里,我們為每個向量分解計算:

ret <- double(1)
ret2 <- NULL
ret2[1] <- .Call("sum_integer64", y, na.rm, ret)
ret2[2] <- .Call("sum_integer64", x, na.rm, ret)
oldClass(ret2) <- "integer64"
ret2
#integer64
#[1] 0  40      <-- only once the value "40", and "0" because of NaNs
sum(ret2, na.rm = na.rm)
#integer64
#[1] 40         <- expected value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM