Assignment #11: Debugging and Defensive Programming in R

For this assignment, we are given the following code:

It is supposed to return outliers of the columns in x, however, when we run this, there us no output. We can find the error by creating a matrix to test the function:

In R, && only checks the first element of a logical vector, while & compares all elements individually. So, replacing && with & makes sure the operation works element by element.

The assignment did not provide a definition for tukey.outlier(), so I had to define this function before testing my corrected code. The main bug in the original function was the use of && instead of &. The && operator only evaluates the first element, whereas & performs element-wise logical comparisons. Replacing it with & fixes the intended operation. The return shows there are no outliers in x. 

Github link here.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *