R: ifelse 把 numeric(0) 变成 NA

R: ifelse turns numeric(0) into NA

有人可以向我解释为什么会发生以下情况吗?

ifelse(TRUE, numeric(0), 1)
> [1] NA

我当然希望 numeric(0)。 我怀疑这是因为 ifelse 是矢量化的,例如以下作品,但我不明白到底发生了什么。

if (TRUE) numeric(0) else 1
#> numeric(0)

您可以访问 ifelse 的实现,即

function (test, yes, no) 
{
    if (is.atomic(test)) {
        if (typeof(test) != "logical") 
            storage.mode(test) <- "logical"
        if (length(test) == 1 && is.null(attributes(test))) {
           #... let's skip this part..
        }
    }
    else test <- if (isS4(test)) 
        methods::as(test, "logical")
    else as.logical(test)
    ans <- test
    len <- length(ans)
    ypos <- which(test)
    npos <- which(!test)
    if (length(ypos) > 0L) 
        ans[ypos] <- rep(yes, length.out = len)[ypos]
    if (length(npos) > 0L) 
        ans[npos] <- rep(no, length.out = len)[npos]
    ans
}
<bytecode: 0x00000123e6b7d3a0>
<environment: namespace:base>

所以,是的,这是因为 ifelse 是矢量化的 - 特别是沿着 条件 - 并且 return 对象 ans 被初始化作为与条件相同长度的向量。

ifelse 个州的描述

ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.

test <- TRUE。有趣的台词是

ypos <- which(test)
rep(numeric(0), length.out = 1)[ypos]

如果您想调整函数使其 returns numeric(0) 在您的情况下,您可以将函数内部的 if(length(yes) == 1) 更改为 if (length(yes) == 0 | length(yes) == 1)。这给你:

ifelse2 <- function (test, yes, no) {
  if (is.atomic(test)) {
    if (typeof(test) != "logical") 
      storage.mode(test) <- "logical"
    if (length(test) == 1 && is.null(attributes(test))) {
      if (is.na(test)) 
        return(NA)
      else if (test) {
    if (length(yes) == 0 | length(yes) == 1) { # Here is what I changed
          yat <- attributes(yes)
          if (is.null(yat) || (is.function(yes) && identical(names(yat), 
                                                         "srcref"))) 
            return(yes)
        }
  }
      else if (length(no) == 1) {
        nat <- attributes(no)
        if (is.null(nat) || (is.function(no) && identical(names(nat), 
                                                          "srcref"))) 
          return(no)
      }
    }
  }
  else test <- if (isS4(test)) 
    methods::as(test, "logical")
  else as.logical(test)
  ans <- test
  len <- length(ans)
  ypos <- which(test)
  npos <- which(!test)
  if (length(ypos) > 0L) 
    ans[ypos] <- rep(yes, length.out = len)[ypos]
  if (length(npos) > 0L) 
    ans[npos] <- rep(no, length.out = len)[npos]
  ans
}

尝试一下:

ifelse2(TRUE, numeric(0), 1)
> [1] numeric(0)

您可以对 no 参数执行相同的操作,如果它在您的情况下也可以是 numeric(0)