-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMod11--Debugging.R
29 lines (29 loc) · 1006 Bytes
/
Mod11--Debugging.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#Module 11 Assignment: Debugging and Defensive Programming
#For this assignment, the code for the tukey_multiple() function is provided with errors.
#It has to be debugged for it to work.
#The tukey.outlier() function is defined ahead of the given code so that it works.
#It returns a vector of logicals indicating outliers.
tukey.outlier <- function(set) {
outlier <- vector(length=length(set))
for (i in 1:length(outlier)) {
if (set[i] > mean(set) + 3*sd(set) || set[i] < mean(set) - 3*sd(set)) {
outlier[i] <- TRUE
}
}
return(outlier)
}
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) }
#Some code is added to test the tukey_multiple() function.
set.seed(252)
sample <- matrix(floor(runif(n=30, max=50)), nrow = 5)
sample
tukey_multiple(sample)