[R] Pedigree / Identifying Immediate Family of Index Animal

Terry Therneau therneau at mayo.edu
Fri Mar 19 16:54:13 CET 2010


On Thu, 18 Mar 2010, Ben Bimber wrote:

> I have a data frame containing the Id, Mother, Father and Sex from
about
> 10,000 animals in our colony.  I am interested in graphing simple
family
> trees for a given subject or small number of subjects.  The basic idea
is:
> start with data frame from entire colony and list of index animals.  I
need
> to identify all immediate relatives of these index animals and plot
the
> pedigree for them.  We're not trying to do any sort of real analysis,
just
> present a visualization of the family structure.  I have used the
kinship
> and pedigree packages to plot the pedigree.  My question relates to
> efficiently identifying the animals to include in the pedigree:


 Your basic idea is sound -- the drawing programs do the same type of
thing and it is very fast.  Your loop is only over 3 generations.  My
version of the function interleaves the up/down steps.
   	Terry Therneau
findKin <- function(id, dadid, momid, index, generations=3) {
    idrow <- match(index, id)
    if (any(is.na(idrow))) stop("Index subject not found")

    for (i in 1:generations) {
        # add parents
        idrow <- c(idrow, match(momid[idrow], id, nomatch=0),
                          match(dadid[idrow], id, nomatch=0))
        idrow <- unique(idrow[idrow>0])  # toss the zeros
        
        # add children
        idrow <- c(idrow, which(match(momid, id[idrow], nomatch=0) >0),
                          which(match(dadid, id[idrow], nomatch=0) >0))
        idrow <- unique(idrow[idrow>0])
        }
    idrow
    }



More information about the R-help mailing list