I have an excel sheet dataframe which contains 22 columns and multiple rows depends upon the output. There is one column named (let's say) column_1
. Now I want to test this whole sheet using asserts but there is a condition, if column_1
has a specific value 1
in any of it's row (let's say row 4), the assertion I wrote won't run for that whole row for all 22 columns, otherwise it will run the assertions for each column as usual.
Dummy Data:
column_1 | column_2 | column_3
NA | "House" | 12
NA | "Plot" | 34
NA | "Office" | 90
1 | "Villa" | 1008
...
...
...
Raw Logic
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)
...
...
...
}
CodePudding user response:
not familiar with the checkmate
package and it's functions. But if I read the question correctly you want to
- check for one specific column if it doesn't have a certain value.
- if this is the case run the rest of your code.
taken the provided code it should be doable with a for loop:
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)
...
...
...
important
- be aware that comments need to be typed with a '#' not double slashes
- as I read your code and my answer, it looks to me like the code will only focus on the specific cell
i
and not the row, do you want to change/assert a cell or the row?
CodePudding user response:
@Omniswitcher answer is the closest one. Here's my final solution.
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)
...
...
...
}
}