title: Count occurences in table and show in rmarkdown, scalability
body: <p>Let's take the following dataframe as an example:</p>
  
  <pre><code>df &lt;- data.frame(state = c('up', 'up', 'up', 'down', 'down', 'up', 'down', 'down'), type = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'))
  </code></pre>
  
  <p>To count how many times each of the possible combinations of A/B and up/down occur I would do as follows:</p>
  
  <pre><code>a1 &lt;- nrow(subset(subset(df, state == 'up'), type == 'A'))
  a2 &lt;- nrow(subset(subset(df, state == 'down'), type == 'A'))
  a3 &lt;- nrow(subset(subset(df, state == 'up'), type == 'B'))
  a4 &lt;- nrow(subset(subset(df, state == 'down'), type == 'B'))
  </code></pre>
  
  <p>And to show that in a Rmarkdown table I would do as follows:</p>
  
  <pre><code>| State/type     |   A    |    B   | 
  |:--------------:|:------:|:------:|
  | Up             | `r a1` | `r a3` |
  | Down           | `r a2` | `r a4` |
  </code></pre>
  
  <p>My question is: is there a better way to do this? This approach becomes very tedious when you have many factor combinations and scales poorly.</p>