I want to use Wilcoxon 2-sided test for two treatments across multiple groups, i.e. there is a before and after treatment (Conc) for each of several sample sites. I want to split the dataset into a list by Site then apply the test so i can have an output for each Site individually, however, i am having trouble setting this up as a function that can repeat.
I have a number of sites (Site) and two levels of treatment (Scenario), with resulting scores (Conc):
'data.frame': 7344 obs. of 6 variables:
$ Site : chr "A" "B" "C" "D" ...
$ Scenario : chr "1" "1" "1" "1" "2" "2" "2" "2" ...
$ Conc : num 4.7727 0.055 0.0552 0.055 0.055 ...
there are multiple Conc data points (~60) within each Site/Scenario combination. The reason i chose a Wilcoxon test is primarily because i have slightly uneven sample numbers between treatments (Scenario) for each Site.
When i use this code for the entire dataset i get a sensible result:
t1 <- wilcox.test(Conc ~ Scenario, data = data.frame)
t1
However, this code doesn't apply the test for each site individually.
I have looked looked at all similar examples i could find (on SO and elsewhere) and this is the best code i could come up with:
t2 = data.frame %>% group_by(Site) %>% do(tidy(wilcox.test(Conc~Scenario, data=data.frame), na.rm=TRUE, equal.var=FALSE))
t2
this code is giving me an output for each site but all test outputs are the same, even the p value:
# A tibble: 107 x 5
# Groups: Site [107]
Site statistic p.value method alternative
<chr> <dbl> <dbl> <chr> <chr>
1 A 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
2 B 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
3 C 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
4 D 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
5 E 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
6 F 6145702 0.690 Wilcoxon rank sum test with continuity correction two.sided
Can anyone see what I'm doing wrong? thanks for your help
lapply(split(data.frame, data.frame$Site), function(x) wilcox.test(Conc ~ Scenario, data = x))
to get a list of Wilcox tests across all your sites – Allan Cameron