Module 11: R Debugging and Defensive Programming
Module 11: R Debugging and Defensive Programming
##Module 11 Debugging
#Libraries
library(dplyr)
library(plyr)
library(tidyverse)
#Raw code with first edit
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) }
##Second edit
tukey_multiple1 <- 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) }
#test block
load(airquality)
ac <- airquality
str(ac)
head(ac)
tukey_multiple(ac)
tukey_multiple1(ac)
#The result of my debugging is that the first error discovered was the return statment return(outlier.vec) was not on its own line
## The second issue that there was is tukey_outlier() is a function that is not defined within the problem. If it was, there would be no issue as R would have a funtion and code to reference back on.
### After this, the function saved, but does not function. After commenting out the undefined tukey_outlier() function, i was able then to achieve a resultant but was more of a list of TRUE statements.
##The undefined function needs to be satisfied so that the function can work properly.
##Final result :
tukey_multiple1(ac)
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[22] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[43] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[64] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[85] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[106] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[127] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[148] TRUE TRUE TRUE TRUE TRUE TRUE
Comments
Post a Comment