title: Make 0/1 character matrix from random phylogenetic tree in R?
body: <p>Is it possible to generate <code>0/1</code> character matrices like those shown below right from bifurcating phylogenetic trees like those on the left. The <code>1</code> in the matrix indicates presence of a shared character that unites the clades.</p>
  
  <p>This code generates nice random trees but I have no idea where to begin to turn the results into a character matrix.</p>
  
  <pre><code>library(ape) # Other package solutions are acceptable
  
  forest &lt;- rmtree(N = 2, n = 10, br = NULL)
  plot(forest)
  </code></pre>
  
  <p>To be clear, I can use the following code to generate random matrices, and then plot the trees.</p>
  
  <pre><code>library(ape)
  library(phangorn)
  
  ntaxa &lt;- 10
  nchar &lt;- ntaxa - 1
  
  char_mat &lt;- array(0, dim = c(ntaxa, ntaxa - 1))
  
  for (i in 1:nchar) {
    char_mat[,i] &lt;- replace(char_mat[,i], seq(1, (ntaxa+1)-i), 1)
  }
  
  char_mat &lt;- char_mat[sample.int(nrow(char_mat)), # Shuffle rows 
                       sample.int(ncol(char_mat))] # and cols
  
  # Ensure all branch lengths &gt; 0
  dist_mat &lt;- dist.gene(char_mat) + 0.5
  upgma_tree &lt;- upgma(dist_mat)
  plot.phylo(upgma_tree, "phylo")
  </code></pre>
  
  <p>What I want is to generate random trees, and then make the matrices from those trees. <a href="https://stackoverflow.com/a/33620418/3832941">This solution</a> does not make the right type of matrix.</p>
  
  <p><strong>Edit for clarity:</strong> I am generating binary character matrices that students can use to draw phylogenetic trees using simple parsimony. The <code>1</code> character represents homologies that unite taxa into clades. So, all rows must share one character (a <code>1</code> across all rows in one column) and some characters must be shared by only two taxa. (I'm discounting autapomorphies.)</p>
  
  <p>Examples:</p>
  
  <p><a href="https://i.stack.imgur.com/rHt8h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rHt8h.png" alt="enter image description here"></a></p>