我想添加一个条件,并测试变量是否存在 2 个类中的任何一个:
test if db present any of 2 class:
示例1
if(!inherits(db, c("bioData", "character") ) ){
cat("\n\t Error:\n")
cat("\t database must be bioData or character class \n")
stop()
}
示例 2,下一个示例可以工作,但它有效吗?
if(!class(db)[1] %in% c("bioData", "character") ){
cat("\n\t Error:\n")
cat("\t database must be bioData or character class \n")
stop()
}
您可以将
inherits
放入 mapply
中。
> if (!any(mapply(inherits, list(X), c('foo', 'bar', 'baz')))) stop('wrong class')
> if (!any(mapply(inherits, list(Y), c('foo', 'bar', 'baz')))) stop('wrong class')
Error: wrong class
数据:
> X <- structure(0, class=c(class(0), c('foo', 'bar', 'baz')))
> Y <- structure(0, class=c(class(0), c('bam', 'bat')))