3.12 NA Check (S)
Purpose: Checks whether the values in a column of the student’s tibble contain NA
values.
Motivation: Some questions involve the removal of NA
values from an existing column (e.g., through filter(!is.na(column_name))
) or a newly constructed column (e.g., through the argument na.rm = T
). This Check detects the presence of NA
values in a column and can hint at their correct removal.
#NA Check Examples
#Example 1
else if(any(is.na(variable_name$column_name))){
test.results[2, 4] <- "The `column_name` has `NA` values. (Any additional feedback as needed.)"
}
#Example 2 (Alternative)
else if(sum(is.na(variable_name$column_name) > 0)){
test.results[2, 4] <- "The `column_name` has `NA` values. (Any additional feedback as needed.)"
}
Technicals
Since the NA Check assumes that the student’s answer has the correct column name, it is essential that the Column Name Check is placed beforehand.
This is a subset of the Value Check.
Like the Calculation Check, the NA Check should be placed before or within the Value Check and does not have to be implemented alongside it.
For examples of how to check multiple columns simultaneously, see below.
#Dynamic NA Check Example (with an example hint)
#Note: Use the all() function instead of any() in examples where some `NA` values might exist in a column, but not every value in the column should be `NA`.
else if(any(all(is.na(variable_name$column_A)) |
all(is.na(variable_name$column_B)) |
all(is.na(variable_name$column_C)))){
wrong_cols <- c(all(is.na(variable_name$column_A)),
all(is.na(variable_name$column_B)),
all(is.na(variable_name$column_C)))
checked_cols <- c("column_A", "column_B", "column_C")
test.results[2, 4] <- paste0(c("The following column(s) have all NA values: ", checked_cols[wrong_cols], " Hint: Did you set na.rm = T?"), collapse = " ")
}