Hi all, I am performing hundreds of kruskal wallis tests and trying to figure out how to create a file of the p.values I obtain. This is the code I use for the tests: A2<-kruskal.test(X2~treatment) A3<-kruskal.test(X3~treatment) A4<-kruskal.test(X4~treatment) A5<-kruskal.test(X5~treatment) A6<-kruskal.test(X6~treatment) A7<-kruskal.test(X7~treatment) A8<-kruskal.test(X8~treatment) A9<-kruskal.test(X9~treatment) ect and I can get the p values from each one individually by: A2$"p.value" I was hoping to figure out how to make one list of all of the p values I need without having to type each one seperately I tried> list1<-list(A1$"p.value":A3$"p.value") > list1[[1]] [1] 0.8419078 but you see this only gives me the first p-value when I was trying to get 3. I also tried> list1<-list(A1$"p.value", A2$"p.value") > list1[[1]] [1] 0.8419078 [[2]] [1] 0.1127509 and this works but I would have to type them all out. If anyone is aware of a shortcut that would be helpful. Thanks
Try something like this (untested): Pvalues <- lapply(1:25, function(x)get(paste("A", i, sep=""))$p.value) On Wed, Nov 12, 2008 at 11:15 AM, <lave0083 at umn.edu> wrote:> Hi all, > I am performing hundreds of kruskal wallis tests and trying to figure out > how to create a file of the p.values I obtain. > > This is the code I use for the tests: > A2<-kruskal.test(X2~treatment) > A3<-kruskal.test(X3~treatment) > A4<-kruskal.test(X4~treatment) > A5<-kruskal.test(X5~treatment) > A6<-kruskal.test(X6~treatment) > A7<-kruskal.test(X7~treatment) > A8<-kruskal.test(X8~treatment) > A9<-kruskal.test(X9~treatment) > ect > > and I can get the p values from each one individually by: > A2$"p.value" > > I was hoping to figure out how to make one list of all of the p values I > need without having to type each one seperately > > I tried >> >> list1<-list(A1$"p.value":A3$"p.value") >> list1 > > [[1]] > [1] 0.8419078 > but you see this only gives me the first p-value when I was trying to get 3. > > I also tried >> >> list1<-list(A1$"p.value", A2$"p.value") >> list1 > > [[1]] > [1] 0.8419078 > > [[2]] > [1] 0.1127509 > > and this works but I would have to type them all out. > If anyone is aware of a shortcut that would be helpful. Thanks > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hello - lave0083 at umn.edu wrote:> Hi all, > I am performing hundreds of kruskal wallis tests and trying to figure > out how to create a file of the p.values I obtain. > > This is the code I use for the tests: > A2<-kruskal.test(X2~treatment) > A3<-kruskal.test(X3~treatment) > A4<-kruskal.test(X4~treatment) > A5<-kruskal.test(X5~treatment) > A6<-kruskal.test(X6~treatment) > A7<-kruskal.test(X7~treatment) > A8<-kruskal.test(X8~treatment) > A9<-kruskal.test(X9~treatment) > etc.Whenever you find yourself typing variations on a theme, there is probably a shortcut. Try, ## begin sample R code ## create test data.frame test <- data.frame(X2 = rnorm(100), X3 = rnorm(100), X4 = rnorm(100), treatment = rep(c("A", "B"), times = 150)) ## apply the kw test to all columns of the data.frame, except treatment ## note: NOT using the model formula version of kr, see ?kruskal.test kr <- lapply(test[!names(test) %in% "treatment"], kruskal.test, g = test$treatment) ## use sapply (basically like lapply) to extract p.value ## note that the trick is that "[" is actually a function ## see ?Extract sapply(kr, "[", "p.value") ## end R code One note of caution, applying hundreds of tests and only recording the <.05 p-values is biased, you'll find chance relationships that aren't really there. In my testing above on random data, I had a few <.05, and one *highly* significant p-value, just be chance. Hope this helps, Erik