Sunday, March 28, 2021

Mod 11: Debugging

Mod-11.utf8


The provided code with the ‘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) }

The code will first throw the following error:

Error: unexpected symbol in:
" for (i in 1:nrow(x))
{ outlier.vec[i] <- all(outliers[i,]) } return"

Often, the first step in debugging is to re-align or re-organize the code for readability. In this case, the error stems from a formatting one. Specifically, return should be placed on its own line and should stand by itself.

The corrected code is shown below.

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)
}



GitHub

Related file(s) can be found at Git Me

No comments:

Post a Comment