title: Creating pivot table in R
body: <p>I know this has been asked numerous times but I've scrolled the internet for the last 2 days and can't find the help I need to create a pivot table or summary table like I could do with excel. I'm completely new to R as in a complete illiterate so I find most examples in R packages help files way too complicated.</p>
  
  <p>I have a list of data ("assemblage") that looks like this</p>
  
  <pre><code>Phase   class  NISP
     &lt;chr&gt;   &lt;chr&gt; &lt;int&gt;
  1      L    Aves    11
  2      L    Fish   128
  3      L Mammals    14
  4      K    Aves    63
  5      K    Fish    30
  6      K Mammals   311
  7      J    Aves   170
  8      J    Fish   327
  9      J Mammals   740
  10     I    Aves    45
  # ... with 18 more rows
  </code></pre>
  
  <p>and I've managed to do a summary/pivottable like table using dcast()</p>
  
  <pre><code>pivot &lt;-dcast(assemblage, Phase ~ class, fun.aggregate = sum, value.var = "NISP", margins = TRUE)
  </code></pre>
  
  <p>with the following result</p>
  
  <pre><code>Phase Aves Fish Indeterminate Mammals (all)
  1      A    1    0             0       6     7
  2      B    2    0             0       3     5
  3      C   58   20             0     255   333
  4      E    5    2             0       5    12
  5      F   14    0             0      17    31
  6      H  121  154             1     784  1060
  7      I   45  110             0     149   304
  8      J  170  327             0     740  1237
  9      K   63   30             0     311   404
  10     L   11  128             0      14   153
  11 (all)  490  771             1    2284  3546
  </code></pre>
  
  <p>But what I need now that I can't figure out is:</p>
  
  <ul>
  <li>replace (all) with "Total"</li>
  <li>order columns in following order ("Phase","Mammals","Fish","Aves","Indeterminate","Total")</li>
  <li>add new columns with percentage of each animal class per phase (percentage of row total).</li>
  </ul>