如何测试多个类

问题描述 投票:0回答:1

我想添加一个条件,并测试变量是否存在 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()
    }
r class
1个回答
0
投票

您可以将

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')))
© www.soinside.com 2019 - 2024. All rights reserved.