[R] pairs

David Winsemius dwinsemius at comcast.net
Mon Nov 16 13:02:53 CET 2009


I'm not convinced it's right. In fact, I'm pretty sure the last step  
taking only the first half of the list is wrong. I also do not know if  
you have considered how you want to count situations like:

3 2 7 4 5 7 ...
7 3 8 6 1 2 9 2 ......

How many "pairs" of 2-7/7-2 would that represent?

-- 
David
On Nov 15, 2009, at 11:06 PM, cindy Guo wrote:

> Hi, David,
>
> The matrix has 20 columns.
> Thank you very much for your help. I think it's right, but it seems  
> I need some time to figure it out. I am a green hand. There are so  
> many functions here I never used before. :)
>
> Cindy
>
> On Sun, Nov 15, 2009 at 5:19 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
> 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
>
> -- 
> David.
>
>
>
> On Nov 15, 2009, at 8:06 PM, David Winsemius wrote:
>
> I could of course be wrong but have you yet specified the number of  
> columns for this pairing exercise?
>
> On Nov 15, 2009, at 5:26 PM, cindy Guo wrote:
>
> Hi, All,
>
> I have an n by m matrix with each entry between 1 and 15000. I want  
> to know
> the frequency of each pair in 1:15000 that occur together in rows.  
> So for
> example, if the matrix is
> 2 5 1 6
> 1 7 8 2
> 3 7 6 2
> 9 8 5 7
> Pair (2,6) (un-ordered) occurs together in rows 1 and 3. I want to  
> return
> the value 2 for this pair as well as that for all pairs. Is there a  
> fast way
> to do this avoiding loops? Loops take too long.
>
> and provide commented, minimal, self-contained, reproducible code.
>                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list