我试图在 R 中使用 ifelse 返回 null。但它会抛出错误消息。任何建议请。
这是我的代码:
cntr1 <- ifelse(unlist(gregexpr("---",path_info[j], fixed = TRUE, useBytes = TRUE)) > 0, 3 * length(unlist(gregexpr("---",path_info[j], fixed = TRUE, useBytes = TRUE))),NULL )
错误信息是:
Error in ifelse(unlist(gregexpr("---", path_info[j], fixed = TRUE, useBytes = TRUE)) > :
replacement has length zero In addition: Warning message:
In rep(no, length.out = length(ans)) :
'x' is NULL so the result will be NULL
我想出了三种不同的返回方法NULL
在类似 ifelse 的场景中。
在这种情况下 b 应该是NULL
当a是NULL
a <- NULL
is.null(a) #TRUE
b <- switch(is.null(a)+1,"notNullHihi",NULL)
b <- if(is.null(a)) NULL else {"notNullHihi"}
b <- unlist(ifelse(is.null(a),list(NULL),"notNullHihi"))
is.null(b) #TRUE for each of them
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)