I have a large dataset with numerous watersheds and ecological sites within those watersheds. I need to compare the ecological site in each watershed against all other data points in the same ecological groups in all other watersheds. I then need to put the first ecological site back into the control population so I can compare the next ecological site.
E.g., Test 1: Watershed A - Ecological Site W vs XYZ
Test 2: Watershed A - Ecological Site X vs WYZ
Text _: Watershed B - Ecological Site W vs XYZ
and so forth through all watershed/ecological site columns. The final output would be a table with a column for watershed, ecological site and then the outputs from DescTools::DunnettTest
Example Data:
set.seed(1)
Watershed <- factor(c("A", "A", "A", "A", "A", "A", "B", "B",
"B", "B", "B", "B", "C", "C", "C", "C", "C", "C"))
EcologicalSite <- factor(c("X", "X", "X", "Y", "Y", "Y","X",
"X", "X", "Y", "Y", "Y", "Z", "Z", "Z","X", "X", "X"))
Cover <- round(rnorm(18,10,3),2)
df <- data.frame(Watershed,EcologicalSite, Cover)
Example output table format:
out <- data.frame(matrix(ncol=6,nrow=0, dimnames=list(NULL,
c("Watershed", "EcologicalSite","diff", "lwr.ci", "upr.ci",
"pval"))))
A brute force method would look like:
AX <- df %>%
filter(EcologicalSite == "X")
AX$Test <- ifelse(AX$Watershed == "A", "Sample", "Control")
AX.Dunnet <- DunnettTest(Cover~Test,data=AX ,control="Control")
AX.Dunnet$Control[1:4]
[1] -2.62166667 -4.85229505 -0.39103829 0.02733156
In short the goal is to create a loop through multiple the multiple ecological sites. I’ve read other posts but none include replacing the first samples group back into the population.
Thank you so much in advance.