<p>Hello, biostars!
I have some problems with miltyPhylo object manipulation and can't understand how to solve it.</p>

<p>I have many trees from MrBayes analysis and want to use it for comparative phylogenetics methods. Trees are ultrametric and UNrooted (also can't understand why, in MrBayes my outgroup sequence was the first in the matrix). 
Than I did following steps:
1) outgroup deletion (species from other group, which I don't want to include in analysis).</p>

<pre><code>&gt;lapply (ntrees [1], is.ultrametric)
$`    TREE * gen.6720000`
[1] TRUE
&gt;lapply (ntrees [1], is.rooted)
$`    TREE * gen.6720000`
[1] TRUE
</code></pre>

<p>Than I looked at one of the trees, it was unrooted.</p>

<p>2)  To root trees for the following analysis</p>

<pre><code>&gt;rtrees&lt;-lapply (ntrees, root, "new outgroup")
&gt; lapply (rtrees [1], is.rooted)
$`    TREE * gen.6720000`
[1] FALSE

&gt; lapply (rtrees [1], is.ultrametric)
$`    TREE * gen.6720000`
[1] FALSE
</code></pre>

<p>I'm new in phylogenetic comparative methods and in R project. So I just can't find a mistake.</p>

<p>Hi Alice, </p>

<p>A couple of things here. </p>

<p>1) the apply family functions are for applying a given function to every member of a list/matrix/vector etc. But you've sliced out the first part of the <code>multiPhylo</code>object (basically a list). So you are only applying the function to the first part. On top of that, there are two ways to slice lists in R, single or double square brackets. Using single brackets will return a list (with the slice indices you give it), double brackets the particular member of this list you are denoting. (see <code>?Extract</code> to get your head around the distinction). The has some unexpected results:</p>

<pre><code>trees &lt;- replicate(10, rtree(10), simplify=FALSE)
plot(trees[[1]]) #a tree, works fine
plot(trees[1]) # a list, R doesn't know how to plot it 
plot(trees[1][[1]])# the first element of the list is a tree, plots fine
</code></pre>

<p>In your example you are applylng the <code>is.rooted</code> and <code>is.ultrametric</code> functions to lists not trees, and that may have similar unexpected results.</p>

<p>To check all trees in a tree object are rooted just drop the slice:</p>

<pre><code>lapply(ntrees, is.rooted) #screeds of TRUEs
all(lapply(ntrees, is.rooted)) #one TRUE
</code></pre>

<p>2) You probably need to specify <code>resolve.root=TRUE</code> when rooting the tree with a single tip. This will add 0-length branch to node below the specified tip (which becomes the root). Check out <code>?root</code> for the details. You can add additional arguments to the function call to apply family functions:</p>

<pre><code>rtrees &lt;- lapply (ntrees, root, "new outgroup", resolve.root=TRUE)
</code></pre>
