I'm trying to write a function that would give me the location of outliers for each column of a data.frame
in a list
object. Therefore, I adapted the function suggested by @timothyjgraham in this post.
FindOutliers <- function(data) {
lowerq = apply(data, 2, quantile)[2,]
upperq = apply(data, 2, quantile)[4,]
iqr = upperq - lowerq
#identify extreme outliers
extreme.threshold.upper = (iqr * 3) + upperq
extreme.threshold.lower = lowerq - (iqr * 3)
result <- list()
for (i in 1:ncol(data)) {
result[[i]]<-which(data[,i] > extreme.threshold.upper[i] | data[,i] < extreme.threshold.lower[i])
}
}
The function works fine when the input data.frame
has only one column, but gives me the following error when using >1 column:
Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) :
undefined columns selected
When I run the code with the same data set outside the function everything works fine.