Jessica
Jessica

Reputation: 461

how to calculate the mean of each treatment across all the replications

I am looking for a way to calculate mean, sd and se values for Weight for each treatment across two replications.

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  

I tried dplyr package but it gives the mean of the treatment for each replication, not both replications combined.

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())))

Thank you for the help!

Upvotes: 1

Views: 535

Answers (1)

akrun
akrun

Reputation: 887223

Based on the OP's code

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)

data

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))

Upvotes: 2

Related Questions