0

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.

viktor_r
  • 701
  • 1
  • 10
  • 21
  • please provide a data set to test … it worked perfectly fine with my data set once I added `return(result)` to the function. – mzuba Jun 13 '17 at 16:08
  • Adding `return(result)` and restarting R fixed the problem. Thanks @mzuba – viktor_r Jun 13 '17 at 16:13

0 Answers0