R:ifelse将numeric(0)转换为NA

6

有人能解释一下为什么会出现以下情况吗?

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

我当然会期望得到 numeric(0)。我猜这是因为 ifelse 可以进行向量化操作,例如下面的代码可以运行,但我不太明白其中的具体过程。
if (TRUE) numeric(0) else 1
#> numeric(0)

1
看起来你不能有一个 numeric(0) 的向量,参见:c(numeric(0), numeric(0))。我猜它试图以可能的方式对其进行向量化。 - cory
4
ifelse()函数返回的向量长度与测试条件相同,因此它不能返回长度为零的向量。 - Ritchie Sacramento
@H 在示例1中,测试的长度不是零吗?因为length(TRUE)是1。 - user11538509
@machine 是的,但 numeric(0) 的长度为0,所以我猜它会执行类似 rep_len(numeric(0), 1) 的操作。 - Sebastian
1
@Sebastian,我只是指出问题不在于测试的长度,正如H1所建议的那样,而在于numeric(0)的长度。 - user11538509
@H 我不确定,这就是为什么我问“示例1中的测试长度不是零吗?” 我当然不想冒犯你或者什么的。很抱歉似乎把你的评论放在了错误的上下文中。这是我的理解方式。 - user11538509
2个回答

4

您可以访问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是向量化的,特别是沿着条件方向,返回对象ans初始化为与条件相同长度的向量。 ifelse的描述如下:

ifelse返回一个与测试形状相同的值,其中填充了从yes或no中选择的元素,具体取决于测试元素是TRUE还是FALSE。

test <- TRUE。有趣的行是:
ypos <- which(test)
rep(numeric(0), length.out = 1)[ypos]

1
为什么在你的例子中将 length.out 参数设置为 0?如果我们使用 test <- TRUE,那么在 ifelse() 中我们有 ans <- test; len <- length(ans); rep(yes, length.out = len)。难道参数不应该设置为 1 吗? - user11538509

1
如果您想调整该函数,使其在您的情况下返回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),则可以使用相同的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接