Skip to content

Instantly share code, notes, and snippets.

@thomasjensen
Created December 13, 2011 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasjensen/1472886 to your computer and use it in GitHub Desktop.
Save thomasjensen/1472886 to your computer and use it in GitHub Desktop.
How to feed a function to the apply function
data <- cbind(rnorm(100),rnorm(100),rnorm(100))
outlier <- function(xrow){
m <- mean(xrow)
devs <- abs(xrow - m)
pos <- which.max(devs)
val <- devs[pos]
out <- c(pos,val)
return(out)
}
out <- apply(data,1,outlier)
out.t <- t(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment