#function to claculate PD using a phylogeny file (phy) and a #community file in the phylocom format (columns: community name, #spp abundance, spp name). Make sure names in community file and #phylogeny match using 'match' function. #Marc Cadotte #Oct 2008 PDcalc<- function (phy,com) { Community<-unique(com[,1])#generate list of community IDs N.coms<-length(Community) species<-phy$tip.label #for output PD<-numeric(N.coms) sp.rich<-numeric(N.coms) for (i in 1:N.coms) { ID<-Community[i]#select community.ID myclade<-com[com[,1]==ID,3] if (length(myclade)==1) { print(paste("Community",ID,"has a single species!")) } sub.tree<-subTree(phy,myclade) PD[i]<-sum(sub.tree$edge.length) sp.rich[i]<-length(sub.tree$tip.label) }#end iter loop results <- data.frame(Community,PD,sp.rich) return(results) }