我有一个excel工作表数据框架,它包含22列和多行,具体取决于输出。有一列名为(比方说(column_1
。现在,我想使用断言测试整个工作表,但有一个条件,如果column_1
在它的任何一行(比如第4行(中有一个特定的值1
,我编写的断言将不会为所有22列的整行运行,否则它将像往常一样为每列运行断言。
伪数据:
column_1 | column_2 | column_3
NA | "House" | 12
NA | "Plot" | 34
NA | "Office" | 90
1 | "Villa" | 1008
...
...
...
原始逻辑
if (excel_sheet[["column_1"]] == "1") {
# the assertion will **not** run for that specific row
} else {
# the assertion will run for that specific row
assert_numeric(x = excel_sheet[["column_1"]], any.missing = T, lower = 1, upper = 1)
assert_numeric(x = excel_sheet[["column_2"]], any.missing = F, lower = 0, upper = 1)
assert_numeric(x = excel_sheet[["column_3"]], any.missing = F)
assert_character(x = excel_sheet[["column_4"]], any.missing = F)
...
...
...
}
不熟悉checkmate
包及其功能。但如果我读对了问题,你想
- 如果某个特定列没有特定值,请检查该列
- 如果是这种情况,请运行其余的代码
采用所提供的代码,它应该可以使用for循环:
for (i in excel_sheet$column_1){
if(i != 1){
# comment
assert_numeric(x = i, any.missing = T, lower = 1, upper = 1)
assert_numeric(x = i, any.missing = F, lower = 0, upper = 1)
assert_numeric(x = i, any.missing = F)
assert_character(x = i, any.missing = F)
...
...
...
重要
- 请注意,注释需要用'#'而不是双斜杠键入
- 当我阅读你的代码和我的答案时,我觉得代码只关注特定的单元格
i
,而不是行,你想更改/断言单元格还是行
@Omniswitcher的答案最接近。这是我的最终解决方案。
for (i in 1:nrow(excel_sheet)) {
if (excel_sheet[["column_1"]][i] != 1) {
assert_numeric(x = excel_sheet[["column_2"]][i], any.missing = F, lower = 0, upper = 1)
assert_numeric(x = excel_sheet[["column_3"]][i], any.missing = F)
assert_character(x = excel_sheet[["column_4"]][i], any.missing = F)
...
...
...
}
}