Assignment 11
For this assignment, I debugged a function that was supposed to identify rows in a numeric matrix where every column is flagged as a Tukey outlier. When I tested the original function on a sample matrix, I got this error: Error in outliers[, j] && tukey.outlier(x[, j]) : 'length = 10' in coercion to 'logical(1)'. This happened because the function used && inside the loop. In R, && is not vectorized, so it only checks the first value of each logical vector instead of comparing all elements. Since outliers[, j] and tukey.outlier(x[, j]) are both logical vectors of length 10, using && caused R to try forcing that full vector into a single logical value.
I fixed the bug by replacing && with &, which performs element-wise logical comparison across the whole vector. After that change, the corrected function ran successfully and returned a logical vector of length 10 with no error. I also added defensive programming checks to make sure the input is actually a matrix and that it is numeric before the function continues. These checks make the function more reliable and help catch bad input early with clear error messages.
Editing r-programming-assignments/README.md at main · macauleywilson/r-programming-assignments



Comments
Post a Comment