1

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!

Jessica
  • 391
  • 1
  • 3
  • 16
  • 3
    Please post the code you used. You likely didn't supply the right grouping variables to `group_by` – divibisan Feb 04 '20 at 19:43
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Show the code you already tried. Be sure to share the data in a reproducible format. You seem to have spaces in some of your values which makes it hard to copy/paste in this format. It's better to share a `dput()` or something similar. – MrFlick Feb 04 '20 at 19:46
  • I have added the code. Thank you! – Jessica Feb 04 '20 at 19:47

1 Answers1

2

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you, there is one problem. For some treatments, there is no value for Rep 2, in that case it calculates mean and no sd or se. How can I fix that? – Jessica Feb 04 '20 at 20:03
  • @Jessica What do you want as output for `sd` and `se` for those cases – akrun Feb 04 '20 at 20:11
  • the code does not give the right mean values for each treatment for each replication this way. I got mean as approximately half of what it should be for each rep using above code. – Jessica Feb 04 '20 at 20:12
  • @Jessica If you can update your post with the expected ouptut, it wioould be easier to crosscheck – akrun Feb 04 '20 at 20:15