Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr summarise_each standard error function

Tags:

r

dplyr

I can summarise my data and calculate mean and sd values using:

summary <- aspen %>% group_by(year,Spp,CO2) %>% summarise_each(funs(mean,sd))

However, I cannot manage to calculate standard error as well. I tried this with no success:

summary <- aspen %>% group_by(year,Spp,CO2) %>% summarise_each(funs(mean,sd,se=sd/sqrt(n())))
like image 684
fede_luppi Avatar asked Nov 28 '22 16:11

fede_luppi


2 Answers

You could do

library(dplyr)
aspen %>% 
    group_by(year,Spp,CO2) %>%
    summarise_each(funs(mean,sd,se=sd(.)/sqrt(n())))

For reproducibility,

data(mtcars)
grpMt <- mtcars %>%
          group_by(gear, carb)

grpMt %>%
     summarise_each(funs(mean, sd, se=sd(.)/sqrt(n())), hp:drat) %>% 
     slice(1:2)
#   gear carb hp_mean drat_mean     hp_sd   drat_sd     hp_se    drat_se
#1    3    1   104.0    3.1800  6.557439 0.4779121  3.785939 0.27592269
#2    3    2   162.5    3.0350 14.433757 0.1862794  7.216878 0.09313968
#3    4    1    72.5    4.0575 13.674794 0.1532699  6.837397 0.07663496
#4    4    2    79.5    4.1625 26.913441 0.5397144 13.456721 0.26985722
#5    5    2   102.0    4.1000 15.556349 0.4666905 11.000000 0.33000000
#6    5    4   264.0    4.2200        NA        NA        NA         NA

which is the same you get with std.error from plotrix

 library(plotrix)
 grpMt %>% 
    summarise_each(funs(mean, sd, se=std.error), hp:drat) %>% 
    slice(1:2)
 #  gear carb hp_mean drat_mean     hp_sd   drat_sd     hp_se    drat_se
 #1    3    1   104.0    3.1800  6.557439 0.4779121  3.785939 0.27592269
 #2    3    2   162.5    3.0350 14.433757 0.1862794  7.216878 0.09313968
 #3    4    1    72.5    4.0575 13.674794 0.1532699  6.837397 0.07663496
 #4    4    2    79.5    4.1625 26.913441 0.5397144 13.456721 0.26985722
 #5    5    2   102.0    4.1000 15.556349 0.4666905 11.000000 0.33000000
 #6    5    4   264.0    4.2200        NA        NA        NA         NA
like image 151
akrun Avatar answered Dec 04 '22 11:12

akrun


You can use std.error function from plotrix package or define your own function first and pass that function name as argument.

    library(plotrix)
    summary <- aspen %>% group_by(year,Spp,CO2) %>% 
summarise_each(funs(mean,sd,std.error)))
like image 38
Koundy Avatar answered Dec 04 '22 13:12

Koundy