0
votes

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 ?

1
Your df is matrix so, apply works okay. If you need to work with lapply, convert the dataset to data.frame Other option would be to use summarise_each from dplyrakrun
@akrun you can make an example if you want to !user1267127

1 Answers

1
votes

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.