A very typical task in data analysis is calculation of summary statistics for each variable in data frame. Standard lapply or sapply functions work very nice for this but operate only on single function. The problem is that I often want to calculate several diffrent statistics of the data. For example assume that we want to calculate minimum, maximum and mean value of each variable in data frame.
The simplest solution for this is to write a function that does all the calculations and returns a vector. The sample code is:
It gives the following result for cars data set:
However, when I work in interactive mode I would prefer to have a function that would accept multiple functions as arguments. I came up with the following solution to this problem:
My multi.sapply function takes a vector as first argument and next one can specify multiple functions that are to be applied to this vector. Applying it to cars data yields:
If function argument is given name it will be used as column name instead of deparsed expression. This functionality is shown by the following example summarizing several statistics of EuStockMarkets data set: