top of page

R Programming Journal Module 11 - Wesley Huang

  • Writer: Wesley Huang
    Wesley Huang
  • Apr 6
  • 1 min read

In this assignment, I worked through a debugging problem in R involving a function that was supposed to identify rows in a numeric matrix that were outliers in every column using the Tukey rule. I first ran the original buggy code on a test matrix so I could reproduce the exact error message: Error in outliers[, j] && tukey.outlier(x[, j]) : 'length = 10' in coercion to 'logical(1)'. This showed that something was wrong with the logical operation being used inside the loop.



After looking at the code more closely, I found that the problem came from using && instead of &. The && operator only checks the first value of a logical vector, but this function needed to compare every value in the column element by element. After replacing it with &, the function worked correctly and returned a logical vector of length 10 without any errors. I also added defensive checks to make sure the input is a matrix and that it contains numeric values, which makes the function safer and easier to debug.

After correcting the function, the output returned a logical vector of length 10 without any errors.


 
 
 

Comments


bottom of page