有人能解释一下为什么会出现以下情况吗?
ifelse(TRUE, numeric(0), 1)
> [1] NA
我当然会期望得到 numeric(0)。我猜这是因为 ifelse 可以进行向量化操作,例如下面的代码可以运行,但我不太明白其中的具体过程。
if (TRUE) numeric(0) else 1
#> numeric(0)
有人能解释一下为什么会出现以下情况吗?
ifelse(TRUE, numeric(0), 1)
> [1] NA
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
是向量化的,特别是沿着条件方向,返回对象ans
初始化为与条件相同长度的向量。
ifelse
的描述如下:
令ifelse返回一个与测试形状相同的值,其中填充了从yes或no中选择的元素,具体取决于测试元素是TRUE还是FALSE。
test <- TRUE
。有趣的行是:ypos <- which(test)
rep(numeric(0), length.out = 1)[ypos]
length.out
参数设置为 0
?如果我们使用 test <- TRUE
,那么在 ifelse()
中我们有 ans <- test; len <- length(ans); rep(yes, length.out = len)
。难道参数不应该设置为 1
吗? - user11538509numeric(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)
,则可以使用相同的方法。
numeric(0)
的向量,参见:c(numeric(0), numeric(0))
。我猜它试图以可能的方式对其进行向量化。 - coryifelse()
函数返回的向量长度与测试条件相同,因此它不能返回长度为零的向量。 - Ritchie Sacramentolength(TRUE)
是1。 - user11538509