Skip to content
Prev 161810 / 398500 Next

creating a file of p.values

Hello -
lave0083 at umn.edu wrote:
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