如何按组计算所有变量的标准误差

发布于 2025-01-21 04:04:18 字数 1014 浏览 3 评论 0原文

我有数据帧包含变量:

    Group   high  weigh age col5

row1   A       12    57   18   AA
row2   C       22    80   29   BB
row3   B       17    70   20   CC
row4   A       13    60   26   DD
row5   D       19    69   25   AA
row6   B       10    15   19   BB
row7   C       20    66   22   CC 
row8   D       13    53   18   DD

我想使用package plotrix的函数std.err或使用其他方法(例如直接计算所有量化的sqrt(length(data [,columt lintar])),该组对组中的所有量化错误都在(例如((例如)第一列),所以我想要的结果是

      Group   se_high   se_weigh  se_age     
row1   A       0.223       0.023    0.1   
row3   B       0.12        0.1      0.12   
row7   C       0.1         0.04     0.09
row8   D      0.05         0.12     0.07

我尝试使用group_by dplyr fubction进行组第一列,然后使用std.error,但我不知道如何组合它们

#this is the dplyr function to calculate the mean by group
library(dplyr)
 data %>%
   group_by(group) %>% 
   summarise_at(vars("A", "B", "C","D"), mean)

,我也想知道如何通过两组(例如,第1列和最后一栏)

谢谢

I have dataframe contain variables :

    Group   high  weigh age col5

row1   A       12    57   18   AA
row2   C       22    80   29   BB
row3   B       17    70   20   CC
row4   A       13    60   26   DD
row5   D       19    69   25   AA
row6   B       10    15   19   BB
row7   C       20    66   22   CC 
row8   D       13    53   18   DD

i want to calulate standar error using the function std.error from package plotrix or using other method ( like calculating directly sd/sqrt(length(data[,column])) of all quantitative error by group in (first column), so the result i want is

      Group   se_high   se_weigh  se_age     
row1   A       0.223       0.023    0.1   
row3   B       0.12        0.1      0.12   
row7   C       0.1         0.04     0.09
row8   D      0.05         0.12     0.07

i tried to use group_by dplyr fubction to group column one and then use std.error but i don't know how to combine them

#this is the dplyr function to calculate the mean by group
library(dplyr)
 data %>%
   group_by(group) %>% 
   summarise_at(vars("A", "B", "C","D"), mean)

i also would like to know how to calculate std.error by two groups ( column 1 and last column 5 for example )

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2025-01-28 04:04:18

你很近!摘要_AT实际上现在已弃用,所以我要做的是:

library(dplyr)
data %>%
  group_by(Group) %>%
  summarize(se_high=plotrix::std.error(high),
            se_weigh=plotrix::std.error(weigh),
            se_age=plotrix::std.error(age))

返回

# A tibble: 4 x 4
  Group se_high se_weigh se_age
  <chr>   <dbl>    <dbl>  <dbl>
1 A         0.5      1.5    4  
2 B         3.5     27.5    0.5
3 C         1        7      3.5
4 D         3        8      3.5

You were close! Summarize_at is actually deprecated now so here's what I'd do:

library(dplyr)
data %>%
  group_by(Group) %>%
  summarize(se_high=plotrix::std.error(high),
            se_weigh=plotrix::std.error(weigh),
            se_age=plotrix::std.error(age))

which returns

# A tibble: 4 x 4
  Group se_high se_weigh se_age
  <chr>   <dbl>    <dbl>  <dbl>
1 A         0.5      1.5    4  
2 B         3.5     27.5    0.5
3 C         1        7      3.5
4 D         3        8      3.5
走过海棠暮 2025-01-28 04:04:18

这是一个一口气的解决方案:

library(dplyr)

df %>%
  group_by(Group) %>%
  summarise(across(where(is.numeric), ~ sd(.x)/ sqrt(length(.x)), .names = "std_{.col}"))

# A tibble: 4 x 4
  Group std_high std_weigh std_age
  <chr>    <dbl>     <dbl>   <dbl>
1 A          0.5       1.5     4  
2 B          3.5      27.5     0.5
3 C          1         7       3.5
4 D          3         8       3.5

Here is a solution to do it in one go:

library(dplyr)

df %>%
  group_by(Group) %>%
  summarise(across(where(is.numeric), ~ sd(.x)/ sqrt(length(.x)), .names = "std_{.col}"))

# A tibble: 4 x 4
  Group std_high std_weigh std_age
  <chr>    <dbl>     <dbl>   <dbl>
1 A          0.5       1.5     4  
2 B          3.5      27.5     0.5
3 C          1         7       3.5
4 D          3         8       3.5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文