Assignment 11: Debugging and Defensive Programming in R

 




The tukey.outlier function identifies outliers in a numeric vector using the Tukey method. It calculates the first (Q1) and third (Q3) quartiles, computes the interquartile range (IQR), and then flags values below Q1 – 1.5*IQR or above Q3 + 1.5*IQR as outliers. The function returns a logical vector indicating which elements are outliers.



The tukey_multiple function determines which rows of a numeric matrix are outliers in all columns. It begins with defensive checks to ensure the input is a numeric matrix, which helps prevent errors during execution.





The function initializes a matrix of TRUE values with the same dimensions as x. It then loops through each column, applying the tukey.outlier function. The element-wise AND operator & ensures that only rows that are outliers in every column remain marked as TRUE.


The function is tested on a 10×5 random numeric matrix generated with rnorm. The set.seed ensures reproducibility. Running tukey_multiple on the test matrix returns a logical vector indicating which rows are outliers in every column.


GitHub link: nbrown022/r-programming-assignments: Naomi Brown

Comments

Popular posts from this blog

Building an R Package for Student Data Analysis: Final Project

Module 5

Assignment #10: Building Your Own R Package