r - 如何检查向量是否是没有长度警告且没有抑制的单个 NA 值
问题描述
我有NA
一个默认函数,但如果不是,则NA
应该是不限于大小 1 的字符向量。我有一个检查来验证这些,但是is.na
当向量是length
大于 1 的字符向量时会产生标准警告。
so_function <- function(x = NA) {
if (!(is.na(x) | is.character(x))) {
stop("This was just an example for you SO!")
}
}
so_function(c("A", "B"))
#> Warning in if (!(is.na(x) | is.character(x))) {: the condition has length >
#> 1 and only the first element will be used
防止我提出警告的一个选项是使用identical
:
so_function <- function(x = NA) {
if (!(identical(x, NA) | is.character(x))) {
stop("This was just an example for you SO!")
}
}
我的问题是,这个函数通常会将加载到 R 中的 Excel 工作表数据作为输入,并且NA
从中生成的值通常是NA_character_
、NA_integer_
和NA_real_
,所以当我真正需要它时identical(x, NA)
通常是。FALSE
TRUE
对于更广泛的上下文,我正在为包创建的 S3 类遇到此问题,下面的函数近似于我如何验证该类的多个属性,即出现警告的时间。因此,我试图避免将警告作为解决方案,因此很想知道解决此问题的最佳实践。
编辑
为了使用例更清晰,这是验证类的属性,我想确保该属性是单个NA
值或任意长度的字符向量:
so_function(NA_character_) # should pass
so_function(NA_integer_) # should pass
so_function(c(NA, NA)) # should fail
so_function(c("A", "B")) # should pass
so_function(c(1, 2, 3)) # should fail
解决方案
长度警告来自使用if
,它需要一个长度为 1 的向量,并且is.na
是向量化的。
您可以使用any
orall
周围将is.na
其压缩为长度为 1 的向量,但可能存在边缘情况,它无法按预期工作,因此我将使用短路评估来检查它的长度为 1 is.na
:
so_function <- function(x = NA) {
if (!((length(x)==1 && is.na(x)) | is.character(x))) {
stop("This was just an example for you SO!")
}
}
so_function(NA_character_) # should pass
so_function(NA_integer_) # should pass
so_function(c(NA, NA)) # should fail
Error in so_function(c(NA, NA)) : This was just an example for you SO!
so_function(c("A", "B")) # should pass
so_function(c(1, 2, 3)) # should fail
Error in so_function(c(1, 2, 3)) : This was just an example for you SO!
另一种选择是改为使用NULL
默认值。
推荐阅读
- php - 是否可以在不使用 cookie 的情况下在 PHP 中实现记住我
- sapui5 - 如何将 SAP UI5 应用程序添加到 Fiori Launchpad?
- python - 为什么 os.getcwd() 在 vs 代码和终端中返回不同的值?
- jenkins - 在 Jenkins 中汇总测试结果
- mongodb - 如何在不知道 Java 中的动态字段类型的情况下查询 mongodb?
- mysql - 如何创建一个空视图(或数组)并稍后在其中插入数据?
- python - 比较两个二维数组的准确性的非断言方法
- python - 使子弹跟随鼠标光标
- python - 如何在一组元组中的每个循环中添加新元素?
- c - 如何在 gdb 中递归设置源文件目录?