gpt4 book ai didi

r - dplyr summarise_each 标准误差函数

转载 作者:行者123 更新时间:2023-12-03 18:11:05 26 4
gpt4 key购买 nike

我可以总结我的数据并使用以下方法计算平均值和标准差值:

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

但是,我也无法计算标准误差。我试过这个没有成功:
summary <- aspen %>% group_by(year,Spp,CO2) %>% summarise_each(funs(mean,sd,se=sd/sqrt(n())))

最佳答案

你可以做

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

为了重现性,
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

这与您从 std.error 获得的 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

关于r - dplyr summarise_each 标准误差函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com