0

I have a simple and a small data set for which I wish to apply a set of functions to each column or variable of the data frame using the sapply function. Below is the code from R Blogs

multi.sapply <- function(...) {
arglist <- match.call(expand.dots = FALSE)$...
var.names <- sapply(arglist, deparse)
has.name <- (names(arglist) != "")
var.names[has.name] <- names(arglist)[has.name]
arglist <- lapply(arglist, eval.parent, n = 2)
x <- arglist[[1]]
arglist[[1]] <- NULL
result <- sapply(arglist, function (FUN, x) sapply(x, FUN), x)
colnames(result) <- var.names[-1]
return(result)
}

Since I am a novice user of R, I would like to know, how can you modify the above code when the data has missing or NA values? So for example:

multi.sapply(mydata,mean, median, min, max)

Works fine but yields NA values for variables that has missing values

The following code however gives me the following error message:

multi.sapply(mydata,mean, median, valid.n, min, max, na.rm = TRUE)

Error in get(as.character(FUN), mode = "function", envir = envir) : object 'FUN' of mode 'function' was not found

Your help would me much appreciated!

user3571389
  • 335
  • 1
  • 5
  • 10
  • 7
    This whole function looks to me like a mess and hard to maintain. I would just do something like `f <- function(x, ...) c(Mean = mean(x, ...), Median = median(x, ...)) ; data.table::setDT(mydata)[, sapply(.SD, f, na.rm = TRUE)]` – David Arenburg Jan 07 '16 at 13:06
  • Thank you very much! Yes, the code that you have provided is more succinct. – user3571389 Jan 07 '16 at 14:37

0 Answers0