Skip to content

pairing data using combn with criteria

6 messages · bjmjarrett, David Winsemius, benjamin_jarrett +1 more

#
Dear All,

I have a dataframe made up of individual beetles consisting of individual
number, family number, mother's family number, father's family number, and
sex of the beetle.  I would like to pair up the individuals for breeding.  I
would, however, like to avoid breeding beetles of the same sex (obviously),
the same family, and with the same mother's family or father's family, to
avoid inbreeding. 

The pairs of the beetles can be done with the function combn(individual, 2). 
I have been trying to use the FUN argument of combn by reducing the options
for combn for each individual by negating the rows that share the same sex,
family, and parents' families, but I have had no success.

Is there an easy way to package all of this into a function for the combn
FUN argument, or is there an alternative way of doing this?

Many thanks in anticipation,

Ben Jarrett



--
View this message in context: http://r.789695.n4.nabble.com/pairing-data-using-combn-with-criteria-tp4649750.html
Sent from the R help mailing list archive at Nabble.com.
#
On Nov 16, 2012, at 6:58 AM, bjmjarrett wrote:

            
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
#
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. 

Many thanks,

Ben



--
View this message in context: http://r.789695.n4.nabble.com/pairing-data-using-combn-with-criteria-tp4649750p4649869.html
Sent from the R help mailing list archive at Nabble.com.
#
On Nov 17, 2012, at 10:07 AM, benjamin_jarrett wrote:

            
Please include context (and _do_ read the Posting Guide.) This is the suggestion I made before:
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"
#
HI,


If the order of individuals are changed or if some individuals are missing, this method may need modification.

ind <- c('1','3','4','8')
? fam <- c('1','2','1','2')
? dat1 <- data.frame(ind,fam)
?combn( row.names(dat1), 2, FUN = function(b){
???????????????? if (dat1[b[1], "fam" ] != dat1[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" 

?row.names(dat1)<-dat1$ind
? combn( row.names(dat1), 2, FUN = function(b){
????????????????? if (dat1[b[1], "fam" ] != dat1[b[2], "fam"] ) { b } else { c(NA,NA) } } )
#???? [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] "1"? NA?? "1"? "3"? NA?? "4" 
#[2,] "3"? NA?? "8"? "4"? NA?? "8" 


A.K.
----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: benjamin_jarrett <bjmjarrett at gmail.com>
Cc: r-help at r-project.org
Sent: Saturday, November 17, 2012 6:36 PM
Subject: Re: [R] pairing data using combn with criteria
On Nov 17, 2012, at 10:07 AM, benjamin_jarrett wrote:

            
Please include context (and _do_ read the Posting Guide.) This is the suggestion I made before:
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"
#
On Nov 17, 2012, at 4:05 PM, arun wrote:

            
If you wanted to account for a situation where the row names do not  
match the 'ind' names, then you could instead use something like this:

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

This should also allow pairs of persons (or chicks or piglets)  with  
the same name (or id)  but from different families to be paired. The  
assignment of duplicate names to row.names might produce some  
surprises in that instance.