## Friday, May 4, 2012

### Ordering trees in a multiPhylo object by the number of tips

Today a R phylogenetics user asks if there is an easy way to sort the trees in a "multiPhylo" object by the number of tips each tree contains. The answer is yes. One way to do it is as follows:

> # first simulate a set of trees with different numbers of tips
> require(phytools)
> trees<-replicate(10,pbtree(n=round(runif(n=1,min=10,max=40))), simplify=FALSE)
> class(trees)<-"multiPhylo"
> trees
10 phylogenetic trees
> # now get the number of tips in each tree
> ntips<-sapply(trees,function(x) length(x\$tip))
> ntips
 35 19 13 34 18 27 18 33 10 25
> # now sort by the decreasing order of ntips
> sorted.trees<-trees[order(ntips,decreasing=T)]

To check & see that we have sorted our trees we can of course just plot them using plot.multiPhylo, but we can also just recalculate the vector ntips from our new list of trees:

> ntips.sorted<-sapply(sorted.trees,function(x) length(x\$tip))
> ntips.sorted
 35 34 33 27 25 19 18 18 13 10

Cool - sorted!

The same user also asked how to pull NULL trees from the list. I suggested one could just do the following:

> trees.no.null<-lapply(trees,function(x) if(!is.null(x)) x))
> class(trees.no.null)<-"multiPhylo"

Evidently, this works - but it just occurred to me that the following simpler option should also do the trick:

> trees.no.null<-trees[!is.null(trees)]

Cool.