Skip to content
Prev 200348 / 398503 Next

pairs

Assuming that the number of columns is 4, then consider this approach:

 > prs <-scan()
1: 2 5 1 6
5: 1 7 8 2
9: 3 7 6 2
13: 9 8 5 7
17:
Read 16 items
prmtx <- matrix(prs, 4,4, byrow=T)

#Now make copus of x.y and y.x

pair.str <- sapply(1:nrow(prmtx), function(z) c(apply(combn(prmtx[z,],  
2), 2,function(x) paste(x[1],x[2], sep=".")) , apply(combn(prmtx[z,],  
2), 2,function(x) paste(x[2],x[1], sep="."))) )
tpair <-table(pair.str)

# This then gives you a duplicated list
 > tpair[tpair>1]
pair.str
1.2 2.1 2.6 2.7 6.2 7.2 7.8 8.7
   2   2   2   2   2   2   2   2

# So only take the first half of the pairs:
 > head(tpair[tpair>1], sum(tpair>1)/2)

pair.str
1.2 2.1 2.6 2.7
   2   2   2   2