假设您想要基于条件setdiff(input, 1:9)构建一个简单的测试。
我如何构造一个
if isnotempty(setdiff(input, 1:9)) stop ("not valid") 当输入为c(3, 12)时停止执行,但当输入为c(2,5,7)时继续执行的语句?非常感谢,伯蒂
发布于 2012-05-19 19:41:04
您可以使用?length
isEmpty <- function(x) {
return(length(x)==0)
}
input <- c(3, 12);
if (!isEmpty(setdiff(input, 1:9))) {
stop ("not valid")
}发布于 2012-05-19 21:01:11
这里有另一个选择identical(x, numeric(0))。下面是一个例子(基本上取自sgibb中的所有内容,并将关键行替换为I‘s lazy):
isEmpty <- function(x) {
return(identical(x, numeric(0)))
}
input <- c(3, 12)
if (!isEmpty(setdiff(input, 1:9))) {
stop ("not valid")
}发布于 2019-10-14 15:57:08
我使用了以下函数:
# 1. Check if 'integer(0)'
is.integer0 <- function(x) {
is.integer(x) && length(x) == 0L
}
# 2. Check if 'numeric(0)'
is.numeric0 <- function(x) {
identical(x, numeric(0))
}
# 3. Check is 'integer0' or 'numeric0'
is.int_num_0 <- function(x) {
is.integer0(x) || is.numeric0(x)
}希望能对大家有所帮助。
https://stackoverflow.com/questions/10664662
复制相似问题