In response to an earlier post, a reader just inquired as to whether there was a simple way to drop the same set of terminal species from a set of trees - say a sample of trees from the posterior distribution. In fact, this can be done a couple of different ways.
A set of trees read by the "ape" functions read.tree() or read.nexus() are stored in memory as a object of class "multiPhylo". This is merely a list of objects of class "phylo" with the class "multiPhylo" assigned. First, let's generate a simple "multiPhylo" object composed of three ten-taxon trees:
Normally this object would be created (with class assigned) by read.tree() or read.nexus(), but for the purposes of illustration, this will do.
Next, let's say we want to prune species "t6" and "t8" from all of the trees in the list. We can do this for any individual tree using drop.tip(), e.g.:
We can prune these taxa all at once from all the trees in our set using the R "base" function lapply(). Here we would just do:
Assigning the class is necessary because lapply() returns a generic list with no class assigned. Now if we type:
we see that we have indeed pruned the desired taxa from each tree in our set. For a longer list, we can easily read the list in from a file, and then send the result object to lapply(...,drop.tip,...).
Note that this can also be done with a simple for() loop, e.g.:
> pruned.trees<-list(); class(pruned.trees)<-"multiPhylo"
> for(i in 1:length(trees))
This is more intuitive, particularly for people experienced in other programming languages like C, but will usually run slower in R.