I am willing to calculate the standardized effect size for soil N among 2 soil water levels (optimal, reduced) and two diversity levels (High versus Low) with 5 replicates each. I am using the formula ES = (soil_N optimal - Soil_N reduced) / (soil_N optimal + soil_N reduced), separately for each replicate and diversity level (High and Low) that I can use to plot a graph. Would it be possible to do this calculation without using loops? I am looking forward to a dplyr/tidyverse solution to this problem. I am very much looking forward to your simple answers.
df<- data.frame(Soilwater = c("optimal", "optimal", "optimal", "optimal", "optimal",
"reduced", "reduced", "reduced", "reduced", "reduced",
"optimal", "optimal", "optimal", "optimal", "optimal",
"reduced", "reduced", "reduced", "reduced", "reduced"),
Diversity = c("High","High","High","High","High","High","High","High","High","High",
"Low", "Low", "Low","Low","Low","Low","Low","Low","Low","Low"),
Soil_N = c(50,45, 49, 48, 49, 69, 68, 69, 70, 67, 79, 78, 79, 78, 77, 89, 89, 87, 88, 89),
Replicate = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5))
For ex., for high diversity, the effect size can be calculated as (50-79)/(50+79) = -0.2248, (45-78)/(45-78) = -0.2682, (49-79)/(49+79) = -0.2343, (48-78)/(48+78) = -0.2381, (49-77)/(49+77) = -0.2222. the average of these 5 replicates is -0.2375. same should be done for low diversity level. I have used the following code but I am getting the value for only 1 replicate and not for all 5 replicates per treatment. Any help would be highly appreciated!
df%>%
rowwise()%>%
dplyr::mutate(Replicate = row_number())%>%
dplyr::group_by(Soilwater, Diversity, Replicate)%>%
dplyr::summarise(Soil_N = mean(Soil_N))%>%
tidyr::spread(key = (Soilwater), value = Soil_N)%>%
dplyr::mutate(ES = (Optimal-Reduced)/(Optimal+Reduced))