using of multiple functions using apply family, aggregation, with etc

I have checked and I found several questions related to this questions multiple functions in a single tapply or aggregate statement R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate

Actually I want to know what is the best way to use multiple functions in one of the above mentioned algorithms.

I try to give an example

# make a simple matrix 
df <- matrix(data=rnorm(10), 10, 5)

# make a function which calculate several properties 
several <- function(x) {
      c(min = min(x), mean = mean(x), max = max(x), median =median(x), sum=sum(x))
   }

# use one of the apply family 
apply(df,2, several)

how would you do that ? is there any other way to make it easier or more practical ?


each in package plyr does the trick for you too:

library(plyr)
df <- matrix(data=rnorm(50), 10, 5)
aaply(df, 2, each(min, mean, max, median, sum)) 

If you want another input/output format, you can play with the different functions from dplyr .

链接地址: http://www.djcxy.com/p/38294.html

上一篇: 申请不会导致像替换为相同的载体

下一篇: 使用应用系列的多种功能,汇总,等等