For the sake of completeness, in case anyone else digs up this post and finds it useful, here are some additional methods I wound up trying besides the use of quartiles shown above:
Taking the Standard deviation and dropping values more than 2* the Standard deviation from the mean (this method may be problematic since the outlier itself affects the mean):
myFunction4 = function(x, na.rm = TRUE, ...) {
rawMean <- mean(x, na.rm = TRUE)
sds <- sd(x, na.rm = TRUE)
lower_cutoff = rawMean - (2*sds)
upper_cutoff = rawMean + (2*sds)
q <- x
q[x < lower_cutoff ] <- NA
q[x > upper_cutoff] <- NA
#return a mean for all values that are not outliers
mean(q, na.rm = TRUE)
}
The MAD method (Median Absolute Deviation):
myFunction5 = function(x, na.rm = TRUE, ...) {
medianX <- median(x, na.rm = FALSE)
#print (medianX)
MAD <- median(abs(x-medianX))
#print(MAD)
Mi <- (.6745 *(x-medianX))/MAD
#print(Mi)
q <- x
q[abs(Mi) > 3.5] <- NA
#return a mean for all values that are not outliers
mean(q, na.rm = TRUE)
}
Just taking the median of the values:
myFunction6 = function(x, na.rm = TRUE, ...) {
median(x, na.rm = FALSE)
}
All of course implemented with the same block of code except for the changing of the function called by FUN=
newtable2 <- aggregate(toy[, -c(1,2)],
by = list(A = toy$A), #Here A could/would be the column header
FUN = myFunction6,
na.rm = TRUE)
newtable2