Skip to content

pairwise comparisions

5 messages · priya, Bert Gunter, David Winsemius +1 more

#
Hi ,
 I am new to R . I am facing difficulty how to make pairwise comparisions.
For example. I have a file which looks like below
   a b c d
x  3 6 7 6
y  7 8 6 5
z  5 4 7 8
Here I need to look for the each pairwise comparisions (ab,ac,ad,bc,bd,cd
for each row)
For instance ,looking at first row, for x i need to look for ab values and
take the min(3,6) >5 ,if its satistfies the count should be noted, (now
count is 0), bc values ,min(6,7) >5 ,count for x =1 etc. Likewise all the
comparisons are made and count is noted for x,y,z. 
Is there any function to make pairwise comparisions in R or any easy way to
do this?
Any help is greatly appreciated. 



--
View this message in context: http://r.789695.n4.nabble.com/pairwise-comparisions-tp4642656.html
Sent from the R help mailing list archive at Nabble.com.
#
1. Be forewarned: There may be special approaches built into specific
packages that are better than "generalist" approaches like the one I
suggest here.

2. The general approach to this sort of thing is with ?outer, if I
understand you correctly.

3. Have you read an Introduction to R (ships with every copy of R)? If
not, stop and read it before posting further to this list, as it may
well contain answers to the questions you wish to pose here.

Cheers,
Bert
On Mon, Sep 10, 2012 at 7:00 AM, priya <jaggari_ash at yahoo.co.in> wrote:

  
    
#
On Sep 10, 2012, at 7:00 AM, priya wrote:

            
How new? Is this still in a file? ... or has it been imported to an R object. If it really still is in a file,  then Bert's suggestion of first reading "Intro to R" rather than posting to R help is on point. If it is an R object, you _should_ have used dput(head(obj)) to provide an appropriate test case for solutions.
Let's assume it's in a matrix of data.frame named X. To get pairwise combinations try:

CX <- combn( colnames(X), 2)

That's going to be a 2 row character matrix. Perhaps this will work but since you provide not example it will remain untested.

apply(CX, 2, function(comb), pmin(X[, comb[1]], X[, comb[2]] ) )
#
On Sep 10, 2012, at 11:03 AM, David Winsemius wrote:

            
The virtues of testing herein demonstrated. There is an inappropriately placed comma right after function(comb)
+ x  3 6 7 6
+ y  7 8 6 5
+ z  5 4 7 8", header=TRUE)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    3    3    6    6    6
[2,]    7    6    5    6    5    5
[3,]    4    5    5    4    4    7
David Winsemius, MD
Alameda, CA, USA
#
HI,
Sorry, there was a mistake.? I forgot to add some code.

Try this:
dat1<-read.table(text="
?? a b c d
x? 3 6 7 6
y? 7 8 6 5
z? 5 4 7 8
",sep="",header=TRUE) 


new1<-sapply(combn(dat1[,1:4],2),`[`,1:3)
?colnames(new1)<-sapply(combn(colnames(dat1),2),`[`,1)
new2<-as.matrix(new1)
?dim(new2)<-c(3,2,6)
list1<-do.call(list,lapply(1:dim(new2)[3],function(i) new2[,,i]))
reslist1<-lapply(list1,function(x) apply(x,1,function(x) ifelse(min(x[1],x[2])>5,1,0)))
names(reslist1)<-c("ab","ac","ad","bc","bd","cd")
res<-data.frame(counts=apply(do.call(cbind,reslist1),1,sum))
?row.names(res)<-row.names(dat1)
?res
#? counts
#x????? 3
#y????? 3
#z????? 1
A.K.


----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: priya <jaggari_ash at yahoo.co.in>
Cc: r-help at r-project.org
Sent: Monday, September 10, 2012 2:03 PM
Subject: Re: [R] pairwise comparisions
On Sep 10, 2012, at 7:00 AM, priya wrote:

            
How new? Is this still in a file? ... or has it been imported to an R object. If it really still is in a file,? then Bert's suggestion of first reading "Intro to R" rather than posting to R help is on point. If it is an R object, you _should_ have used dput(head(obj)) to provide an appropriate test case for solutions.
Let's assume it's in a matrix of data.frame named X. To get pairwise combinations try:

CX <- combn( colnames(X), 2)

That's going to be a 2 row character matrix. Perhaps this will work but since you provide not example it will remain untested.

apply(CX, 2, function(comb), pmin(X[, comb[1]], X[, comb[2]] ) )