title: How to create a count column for each category in R
body: <p>I have a dataframe like this:</p>
  
  <pre><code>ID &lt;- c("A","A","B","B","A","B","B","B","A","A","A","A","B","B","A","A","A","B","B","B")
  Measurement &lt;- c("Len","Len","Len","Wid","Ht","Ht","Wid","Len","Ht","Ht"
                   ,"Wid","Ht","Len","Ht","Wid","Len","Wid","Ht","Len","Wid")
  STATUS &lt;- c("FAIL","FAIL","FAIL_AVG_HIGH","FAIL","FAIL","FAIL_AVG_HIGH","FAIL#Pts","FAIL","FAIL_AVG_LOW","FAIL"
              ,"FAIL#Pts","FAIL","FAIL_AVG_HIGH","FAIL","FAIL","FAIL_AVG_LOW","FAIL","FAIL_AVG_LOW","FAIL","FAIL#Pts")
  df1 &lt;- data.frame(ID,Measurement,STATUS)
  df1 &lt;- within(df1, MEAS_ID &lt;- paste(Measurement, ID, sep=' '))
  </code></pre>
  
  <p>I am trying to create a column with individual fail counts &amp; total fail counts for each category. My desired output looks like this </p>
  
  <pre><code>   ID Measurement        STATUS Count Count_total MEAS_ID
  1   A         Len          FAIL     2           3   Len A
  2   A         Len  FAIL_AVG_LOW     1           3   Len A
  3   A          Ht          FAIL     3           4    Ht A
  4   A          Ht  FAIL_AVG_LOW     1           4    Ht A
  5   A         Wid          FAIL     2           3   Wid A
  6   A         Wid      FAIL#Pts     1           3   Wid A
  7   B         Len          FAIL     2           4   Len B
  8   B         Len FAIL_AVG_HIGH     2           4   Len B
  9   B          Ht          FAIL     1           3    Ht B
  10  B          Ht FAIL_AVG_HIGH     1           3    Ht B
  11  B          Ht  FAIL_AVG_LOW     1           3    Ht B
  12  B         Wid          FAIL     1           3   Wid B
  13  B         Wid      FAIL#Pts     2           3   Wid B
  </code></pre>
  
  <p>I tried calculating the count this way but seems to not work as desired. </p>
  
  <pre><code>df1 &lt;- within(df1, { count &lt;- ave(STATUS, MEAS_ID, FUN=function(x) length(unique(x)))})
  </code></pre>