首页 > 解决方案 > 为什么 ifelse 和 if 语句不一样?

问题描述

给定数据:

condition <- rep(TRUE, 10)
varExist <- c(FALSE, rep(TRUE, 9))
cond <- list(c(rep(TRUE, 6) ,rep(FALSE, 4)))

有条件时:

condition <- if(varExist[1]){
  condition
}else{
  condition & !cond[[1]]
}

然后我想将if语句转换为ifelse语句:

condition <- ifelse(varExist[1], condition, condition & !cond[[1]])

但结果不同:预期:

> condition
 [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

但是还有:

> condition
[1] FALSE

标签: r

解决方案


推荐阅读