Reputation: 433
I have a data table in r called tbl. I want to perform a wilcox test to compare every column with every other column in the table and store the p-values in a new table or matrix.
To compare a single pair of columns (1 and 2 in this case) I would normally just do this:
wilcox.test(tbl[,1],tbl[,2],alternative="t",paired=FALSE)
Any help is greatly appreciated.
Upvotes: 4
Views: 1955
Reputation: 37889
One way is to store the 2-way combinations of your tbl
and then use lapply
to calculate the p.values. Something like this:
#example data.frame
df <- data.frame(a = runif(10), b= runif(10), c=runif(10))
#calculate combinations of names
combinations <- as.data.frame(combn(names(df), 2), stringsAsFactors=FALSE)
#use the above combinations and calculate the wilcox.test to get the p.values
lapply(combinations, function(x) {
wilcox.test(df[,x[1]] , df[, x[2]], alternative="t", paired=FALSE)$p.value
})
Output:
$V1
[1] 0.8534283
$V2
[1] 0.2175626
$V3
[1] 0.3526814
Upvotes: 8