Assignment #11 - Debugging and Defense programming in R

This week, we learned about debugging and defensive programming in R. We were provided with the code below and asked to test it on a matrix to determine if any issues arose when running it. 


The initial error code that RStudio displayed was:

Error in tukey_multiple(test_mat) : 

  could not find function "tukey_multiple"

When running the test matrix below, I discovered two bugs in the code.

set.seed(123)
test_mat <- matrix(rnorm(50), nrow = 10)
tukey_multiple(test_mat)

The first issue was that tukey.outlier( ), had not yet been defined. I first defined that function using these lines to ensure that R knew what functions I was trying to call. 

tukey.outlier <- function(x) {

  q <- quantile(x, c(0.25, 0.75))

  H <- 1.5 * IQR(x)

  x < (q[1] - H) | x > (q[2] + H)

}

The second issue, came from the use of && in this line "outliers[, j] <- outliers[, j] && tukey.outlier(x[, j])". The issue with this is that it is scalar logical and only inspects the first element of a logical vector, returning a single TRUE/FALSE. Therefore, instead of inspecting all of the elements in the vector, it was only inspecting the first and ignoring the others. I then corrected the line to have only a single &, and the corrected code looked like this:


with a corrected output on test_mat returning a logical vector length 10:

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE




GitHub link: https://github.com/hannahcardenas4/r-programming-assignments/tree/main/R_Programming_Fall2025_Cardenas_Hannah/Assignment_11_Debugging_and_Defense

Comments

Popular posts from this blog

Assignment #5: Matrix Algebra in R

Final Project: disordR

Assignment #9 - Visualization in R – Base Graphics, Lattice, and ggplot2