如何计算所有重复项中每种处理的平均值

时间:2020-02-04 19:41:53

标签: r

我正在寻找一种方法来计算两次重复中每种治疗的权重的均值,sd和se值。

Treatment    Rep Weight  
Line 1  1   NA  
Line 1  1   NA  
Line 1  1   NA  
Line 1  1   NA  
Line 2  1   26  
Line 2  1   26  
Line 2  1   26  
Line 2  1   27  
Line 1  2   26  
Line 1  2   28  
Line 1  2   26  
Line 1  2   25  
Line 2  2   24  
Line 2  2   26  
Line 2  2   25  
Line 2  2   NA  

我尝试了dplyr软件包,但它给出了每个复制的平均处理量,而不是两个复制的总和。

Data1 %>% group_by(Treatment, Rep) %>% summarise_at(vars(-group_cols()), list(mean = ~mean(Weight, na.rm = TRUE), 
       sd = ~sd(Weight, na.rm = TRUE), se= ~sd(Weight, na.rm = TRUE)/sqrt(n())))

谢谢您的帮助!

1 个答案:

答案 0 :(得分:2)

基于OP的代码

df1 %>%
   group_by(Treatment, Rep) %>% 
   summarise_at(vars(-group_cols()), list(mean = ~mean(Weight, na.rm = TRUE), 
      sd = ~sd(Weight, na.rm = TRUE),
      se= ~sd(Weight, na.rm = TRUE)/sqrt(n()))) %>% 
   summarise_at(vars(mean:se), mean, na.rm = TRUE)

数据

df1 <- structure(list(Treatment = c("Line 1", "Line 1", "Line 1", "Line 1", 
"Line 2", "Line 2", "Line 2", "Line 2", "Line 1", "Line 1", "Line 1", 
"Line 1", "Line 2", "Line 2", "Line 2", "Line 2"), Rep = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
    Weight = c(NA, NA, NA, NA, 26L, 26L, 26L, 27L, 26L, 28L, 
    26L, 25L, 24L, 26L, 25L, NA)), class = "data.frame", row.names = c(NA, 
-16L))