With a dummy dataset:
Species Var1 Var2 Var3 a 1 2 3 a 4 5 6 b 7 8 9 b 10 11 12
I have several types and about 50 variables (Var50). I would like to perform a unidirectional Anova for a pairing variable of grouping (Species) for each response variable and get an output of frequencies that are statistically significant at 95% CI, for example. I started writing a function to do it like this:
data<-read.table("example.txt", header=T, sep="\t") function(y){ for(y in 2:50) anova.r<-aov(y~Species, data = data) result<-TukeyHSD(anova.r, conf.level = 0.95) f.result ## I cannot figure out how to extract the "p adj" from the results f.result<-sum(prob.result>=0.05) write.table(f.result, file = "anova95.csv", sep = ",", col.names = FALSE, append=TRUE) }
Ultimately, I would like the final table (dummy answers) to look like
Var1 Var2 Var3......Var50 Frequency at 95% CI 106 200 45 246
I know that I can use [[]]
to access the data in the Tukey test results. I tried using tukey.results[[1]][,1]
until tukey.results[[1]][,3]
no avail. tukey.results[[1]]
returns all columns from the Tukey test.
Also, I think I might have to use cbind
somewhere in the function to get the data in the corresponding columns. Or I thought it would be possible to use the apply
command, but I donโt know how to keep the grouping variable constant when the response variable changes at each iteration.
Any suggestions would be greatly appreciated.
source share