Home > Mobile >  List of mean, median and sd for a given variable in R [duplicate]
List of mean, median and sd for a given variable in R [duplicate]

Time:10-07

Is it possible to give the argument FUN in apply more than one argument? Im trying to get a list of the mean, median and sd of a given variable in any dataframe. Example: apply(X = mtcars, MARGIN = mtcars[i], FUN = c(mean, median, sd) , where i is the variable (or column) of interest. Im not sure if this is the method to use, if not, please guide me to a more suitable method. Thanks!

I want something like this:

data(mtcars)
$mpg 
mean median st
20.09 19.2 6.03   

CodePudding user response:

The easiest way to do this is with summary:

summary(mtcars$mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.43   19.20   20.09   22.80   33.90 

Alternatively, you could use sapply and give this a list of functions:

sapply(list(mean = mean, median = median, sd = sd), function(x, y) x(y), mtcars$mpg)
     mean    median        sd 
20.090625 19.200000  6.026948

Here, x refers to the function in the list, and y is the mtcars column. The final argument is just the actual column you want your function to apply to.

CodePudding user response:

You may try

mtcars_summary <- apply(mtcars, 2, function(x){
  c(mean = mean(x), sd = sd(x), med = median(x))
}) 

           mpg      cyl     disp        hp      drat        wt      qsec        vs        am      gear   carb
mean 20.090625 6.187500 230.7219 146.68750 3.5965625 3.2172500 17.848750 0.4375000 0.4062500 3.6875000 2.8125
sd    6.026948 1.785922 123.9387  68.56287 0.5346787 0.9784574  1.786943 0.5040161 0.4989909 0.7378041 1.6152
med  19.200000 6.000000 196.3000 123.00000 3.6950000 3.3250000 17.710000 0.0000000 0.0000000 4.0000000 2.0000

CodePudding user response:

The package {dplyr} provides a very nice way of doing this with across():

library(dplyr)

mtcars %>% 
    summarise(across(mpg, list(mean = mean, median = median, sd = sd)))
#>   mpg_mean mpg_median   mpg_sd
#> 1 20.09062       19.2 6.026948
  • Related