title: Extracting all value from a data set based for every categorical numerical value in R
body: <p>I have a data set with an arbitrary number of row and two columns:a and b. I would like to find the number of a values for a specific value of b. If given the data set below I would want a1 = 2, a2 = 1 for a set value of b1.  </p>
  
  <pre><code>  a   b
    1   1
    1   1
    2   1
    2   2
    3   2
    3   2
  </code></pre>
  
  <p>Code that I've tried and works: </p>
  
  <pre><code> data[a == 1 &amp; b == 1, list(b = length(b))]
  </code></pre>
  
  <p>Code that doesn't work:</p>
  
  <pre><code>data[a == c(1,2) &amp; b == 1, list(b = length(b))]
  </code></pre>
  
  <p>How can I get all values of a for a set b value?  </p>
  
  <p>Expected data output:</p>
  
  <pre><code>    b1 
  a1  2
  a2  1
  a3  0
  etc.     
  </code></pre>
  
  <p>Code that works thanks to akrun: </p>
  
  <pre><code>library(data.table)
  table(as.integer(data$a),data$b=='b1')[,2]
  </code></pre>
  
  <p>Make sure your <code>[,2]</code> matches your 'b' column.<br>
  Also, <code>as.integer()</code> ranks the values in order. </p>