[R] applying wilcox.test to every combination of rows in a matrix (pairwise)

Ulrik Stervbo ulrik.stervbo at gmail.com
Wed Dec 9 14:17:29 CET 2015


Could you use expand.grid and loop over each returned row?

Best,
Ulrik

On Wed, 9 Dec 2015 at 13:55 debra ragland via R-help <r-help at r-project.org>
wrote:

> Hello All,
>
> I have written the following loop which will apply/split the same vector
> of numbers (pc1.eigv) to each (logical) row of a matrix and run a
> wilcox.test on those values that line up with TRUE and those that line up
> with FALSE. It works fine. However, I am now interested in using the same
> vector and (logical)matrix run the wilcox.test only this time I would like
> information about pairs of rows (not just single rows as it already does).
>
> The loop:
> n.iteration=dim(as.matrix(p))[1]
> n.test= rep(NA, n.iteration )
> for( i in 1:n.iteration ){  ## i=1
> i_spl<-split(pc1.eigv, p[i,])
> if( sum(p[i,])==15 | sum(p[i,])==0) { n.test[i]=NA  }
> if( sum(p[i,])!=15 & sum(p[i,])!=0) {
> testout=wilcox.test(i_spl$'TRUE', i_spl$'FALSE')
> n.test[i]=testout$p.value     }
> }
>
>
> some sample data
> p<-matrix(c(rep(c(F,T,F),3), rep(c(T,F,T),3), rep(c(T,T,F),3),
> rep(c(F,F,T),3)))
>
> pc1.eigv<-runif(4, 1.0, 2.0)
>
> After some searching I thought that perhaps the combn function would help
> me (i.e. combn(p)) for the same loop but I get an error.
>
> Can anyone help with this?
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list