Dear Colin,
Try this:
# Data
DF <- structure(c(0, 0, 0, 4, 27, 0, 1, 0, 7, 16, 1, 0, 0, 16, 44,
0, 0, 0, 2, 39), .Dim = c(5L, 4L), .Dimnames = list(c("L01.1",
"L01.2", "L01.3", "L01.4", "L01.5"), c("P01", "P02", "P03", "P04"
)))
# Distances
Names<-t(combn(colnames(DF),2))
apply(Names,1,function(x) JostD(x[1],x[2], DF))
HTH,
Jorge
On Fri, Mar 27, 2009 at 1:33 PM, Colin Garroway <[email protected]>wrote:
> 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]]
>
> ______________________________________________
> [email protected] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.