Posts

Showing posts from April, 2026

Assignment #11: Debugging and Defensive Programming in R

 Debugging and Defensive Programming in R In this assignment, the goal is to reproduce an error, diagnose the issue, correct the code, and apply defensive programming techniques. Reproducing the Error I began by running the provided buggy function on a test matrix: set.seed(123) test_mat <- matrix(rnorm(50), nrow = 10) tukey_multiple(test_mat) This produced the following warning message: Warning message: In outliers[, j] && tukey.outlier(x[, j]) :   'length(x) = 10 > 1' in coercion to 'logical(1)' Diagnosing the Bug The issue comes from this line in the function: outliers[, j] <- outliers[, j] && tukey.outlier(x[, j]) The operator && only evaluates the first element of a logical vector. However, the function tukey.outlier() returns a logical vector corresponding to each row of the matrix. Because of this, only the first value was being checked, which caused incorrect behavior and triggered a warning. To correctly compare all elements,...