library(ape) trees<-read.tree("...") # read tree file containing multiple (2) trees - read.tree uses Newick, use the read.nexus command for nexus sp.list<-read.table("...",header=F)#read in table of species names and community ID (from 1 to number of communities) - no header attach(sp.list) cladeID<-unique(sp.list[,2])#generate list of community IDs communities<-length(cladeID) PD<-numeric(communities) sp.rich<-numeric(communities) iter<-length(trees)# number of trees in file output<- data.frame() for (j in 1:iter){ solo.tree<-trees[[j]]#select one tree at a time from file tree.iter<-j species<-as.list(solo.tree$tip.label)#list of species in tree for (iter in 1:communities) {#run for all species lists ID<-cladeID[iter]#select community.ID myclade<-sp.list[sp.list[,2]==ID,]#get list of species in that community dropme<-species[!species %in% myclade[,1]]#species in tree but not in community sub.tree<-drop.tip(solo.tree,dropme) PD[iter]<-sum(sub.tree$edge.length) sp.rich[iter]<-length(sub.tree$tip.label) }#end iter loop results <- data.frame(tree.iter,cladeID,PD,sp.rich) output<-rbind(output,results) }#loop to next tree #OUTPUT RESULTS output write.table(output, file="...", col.names=T, row.names=F, quote=F, sep="\t")