Module # 11 Debugging and defensive programming in R


Issaiah Jennings

Module # 11 Debugging and defensive programming in R


The code below contains a 'deliberate' bug!

tukey_multiple <- function(x) {
outliers <- array(TRUE,dim=dim(x))
for (j in 1:ncol(x))
{
outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
}
outlier.vec <- vector(length=nrow(x))
for (i in 1:nrow(x))
{ outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }

> Find the bug and fix it.



tukey_multiple <- function(x) {

outliers <- array(TRUE, dim = dim(x))

for (j in 1:ncol(x)) {

outliers[, j] <- outliers[, j] & tukey.outlier(x[, j])

}

outlier.vec <- vector(length = nrow(x))

for (i in 1:nrow(x)) {

outlier.vec[i] <- all(outliers[i, ])

}

return(outlier.vec)

}
I fixed a bug in the tukey_multiple function. I was using && for comparisons, but I needed & instead. After the fix, the function worked correctly.

Comments

Popular posts from this blog

Final project - create your own R Package

Module # 1 assignment(R)

Module # 4 Programming structure in R