Hello,

I have written a small function ('JostD' based upon a recent molecular
ecology paper) to calculate genetic distance between populations (columns in
my data set).  As I have it now I have to tell it which 2 columns to use (X,
Y).  I would like it to automatically calculate 'JostD' for all combinations
of columns, perhaps returning a matrix of distances.  Thanks for any help or
suggestions.
Cheers
Colin



Function:

JostD <- function(DF, X, Y) {

    Ni1 <- DF[,X]
    Ni2 <- DF[,Y]

    N1 <- sum(Ni1)
    N2 <- sum(Ni2)

    pi1 <-Ni1/N1
    pi2 <-Ni2/N2
        pisqr <- ((pi1+pi2)/2)^2

    H1 <- 1 - sum(pi1^2)
    H2 <- 1 - sum(pi2^2)
    Ha <- 0.5*(H1+H2)

    Da <- 1/(1-Ha)
        Dg <- 1/sum(pisqr)
        Db <- Dg/Da
        D <- -2*((1/Db) - 1)
    D
}

Sample data:

e<-c(0,0,0,4,27)
r<-c(0,1,0,7,16)
t<-c(1,0,0,16,44)
y<-c(0,0,0,2,39)
df<-cbind(e,r,t,y)
rownames(df)<-q
colnames(df)<-w

> df
      P01 P02 P03 P04
L01.1   0   0   1   0
L01.2   0   1   0   0
L01.3   0   0   0   0
L01.4   4   7  16   2
L01.5  27  16  44  39

> JostD(df, 1, 2)
[1] 0.0535215

> JostD(df, 1, 3)
[1] 0.02962404
>





-- 
Colin Garroway (PhD candidate)
Wildlife Research and Development Section
Ontario Ministry of Natural Resources
Trent University, DNA Building
2140 East Bank Drive
Peterborough, ON, K9J 7B8
Canada

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to