James Curran
James Curran

Reputation: 1294

Apply a list of functions to a single matrix and retain the function names

My ultimate goal is to collect a list of summary stats for the columns of a data.frame or a matrix. For example, if I had a data.frame with three variables/columns I might want to know the mean, median and standard deviation of each column. What I would like to do is have a function that takes an arbitrary list of functions. I know something like this will do the trick

summarise = function(x, stats = list(mean, median, sd, length)){
     lapply(stats, function(stat){apply(x, 2, stat)})
}

but how can I label the elements of the results with the name of the statistic/function applied? For example, if x is

x = matrix(1:9, nc = 3)

then how could I write my function summarise so that I get

> summarise(x)
$mean
[1] 2 5 8

$median
[1] 2 5 8

$sd
[1] 1 1 1

$length
[1] 3 3 3

back as the result?

Upvotes: 1

Views: 26

Answers (1)

akrun
akrun

Reputation: 887241

Simple option is to have a named output in the OP's summarise function

summarise <- function(x, stats = list(mean = mean, median = median,
       sd = sd, length = length)){
         lapply(stats, function(stat){apply(x, 2, stat)})
   }

summarise(x)
#$mean
#[1] 2 5 8

#$median
#[1] 2 5 8

#$sd
#[1] 1 1 1

#$length
#[1] 3 3 3

Or if we use dplyr::lst or purrr::lst, it would automatically rename the list elements

summarise <- function(x, stats = dplyr::lst(mean, median,
       sd, length)){
         lapply(stats, function(stat){apply(x, 2, stat)})
   }

summarise(x)
#$mean
#[1] 2 5 8

#$median
#[1] 2 5 8

#$sd
#[1] 1 1 1

#$length
#[1] 3 3 3

Upvotes: 1

Related Questions