ali 97
ali 97

Reputation: 3

Trying to get summary variables into a table

So I'm working on a project that looks at the spaces of different car parking spaces. So essentially, I would like the variables from summary (mean, quantile, IQR, sd, max, min, median) into a table that when I run it is comes up as a table with all the different variables for each of my Carpark spaces.

I've tried the following code (I saw it in another Stack Overflow question)

newlist <-list(mean, quantile, IQR, sd, max, min, median)
val <- lapply(newlist, function(fn) fn(densities))

But the error that comes up is that fn(densities) is not found.

Upvotes: 0

Views: 55

Answers (1)

DSGym
DSGym

Reputation: 2867

I think you want something like this:

summary <- function(x) {
  funs <- c(mean, median, sd, mad, IQR)
  lapply(funs, function(f) f(x, na.rm = TRUE))
}

Only for numeric variables:

sapply(mtcars, function(x) { if(is.numeric(x)) summary(x) })

Upvotes: 1

Related Questions