Module 11: Debugging and Defensive Programming in R
For this assignment, I debugged a function that was supposed to identify rows in a numeric matrix that were outliers in every column using the Tukey rule. I first reproduced the bug by creating a test matrix with random values and running the original function. When I ran the function, I got a warning showing that the logical expression was not being handled correctly. The issue was caused by using && inside the loop. In R, && only checks the first element of a logical vector, but this function needed an element-wise comparison across all rows. Because of that, the code was not correctly updating the outlier matrix. To fix the problem, I replaced && with & . The & operator compares vectors element by element, which matches the purpose of the function. After making that change, the corrected version ran without warnings and returned a logical vector of length 10. I also added defensive programming checks at the top of the function to make sur...