[R] setting pairwise comparisons of columns

peter dalgaard PDalgd at gmail.com
Thu Apr 14 10:38:49 CEST 2011


On Apr 13, 2011, at 21:32 , Louis Plough wrote:

> Hi,
> I have a number of genes (columns) for which I want to examine pairwise
> associations of genotypes (each row is an individual)...For example (see
> data below), I would like to compare M1 to M2, M2 to M3, and M1 to M3 (i.e.
> does ac from M1 tend to be found with bc from M2 more often than expected.)
> Down stream I will be performing chi square tests for each pair.
> 
> But I am looking for a way to set all pairs of genes (order doesn't matter,
> so with 3 genes, there are 3 comparisons, 4 genes=6 comparisons) in a new
> data.frame or matrix so that I can then test each pair with a chi-square
> test in a loop.
> 
> Below is some sample data of the form I will be using.
> 
>> lets<-c("ab","ac","bc","bd")
>> epi<-data.frame(cbind("M1"= c(sample(lets,10,
> replace=TRUE)),"M2"=c(sample(lets,10,replace=TRUE)), "M3"=c(sample(lets,10,
> replace=TRUE))))
>> print(epi)
>   M1 M2 M3
> 1  ac bc bd
> 2  ac ac bd
> 3  bd bd bd
> 4  ab ac bd
> 5  ac bc bd
> 6  bd bd bc
> 7  ab ac ab
> 8  bc bd ab
> 9  bd ab ac
> 10 bc bc bd
> 
> I tried a for loop to set each column against the others, but get errors for
> undefined columns selected:
> 
> for(i in 1:3) {
> k=i+1
> j=k
> for(j in k:3){
> epi3=cbind("A"=epi[,i],"B"=epi[,j])
> 
> print(epi3)
> }
> ...

> 10 bc bd
> Error in `[.data.frame`(epi, , j) : undefined columns selected
> 
> 
> I get the output in the right format, but with errors, and the actual data
> frame epi 3, has only one column,
> 
> 
> Im sure this is a simple fix...any ideas? Could I use combn instead?

Well, for i==3, your inner loop is for (j in 4:3), and R is not C, so 
this runs backwards from 4. The quick fix is (I think k is superfluous)
 
for (i in 1:2)
   for (j in (i+1):3)	

However, also check pairwise.table() for a generic solution 
(including adjustment for multiple testing)

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list