[R] pairing data using combn with criteria

David Winsemius dwinsemius at comcast.net
Sun Nov 18 00:36:03 CET 2012


On Nov 17, 2012, at 10:07 AM, benjamin_jarrett wrote:

> Hi David,
> 
> Thanks for replying. Unfortunately I can't get it to work. Here is some
> (very simplified) data to help illustrate my problem.
> 
> ind <- c('1','2','3','4')
> fam <- c('1','2','1','2')
> data <- data.frame(ind,fam)
> 
> ind is the unique ID for each individual, and fam is which family the
> individual came from. Using combn(ind, 2) matches all of the individuals. Is
> there any way I could get combn to pair individuals up based on a different
> family number, so with the above data individual 1 would be paired with
> individual 2 or 4. 

Please include context (and _do_ read the Posting Guide.) This is the suggestion I made before:

> See if this helps:
> 
> combn( 1:5, 2, FUN = function(b){ 
>                 if (max (b) < 4 ) { b } else { c(NA,NA) } } )
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]    1    1   NA   NA    2   NA   NA   NA   NA    NA
> [2,]    2    3   NA   NA    3   NA   NA   NA   NA    NA


And this is how to apply it to the example:

 combn( row.names(data), 2, FUN = function(b){ 
                 if (data[b[1], "fam" ] != data[b[2], "fam"] ) { b } else { c(NA,NA) } } )

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "1"  NA   "1"  "2"  NA   "3" 
[2,] "2"  NA   "4"  "3"  NA   "4" 

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list