title: How to set a column to the number of rows based on a condition in the same row?
body: <p>Basically, I want to do this:</p>
  
  <pre><code>dt &lt;- data.table(category=c("A","A","A","B","B","C"))
  dt$count &lt;- nrow(dt[dt$category == dt$category])
  </code></pre>
  
  <p>That is, add a column "count" to the datatable, and set it to 3 for all "A" rows, 2 for all "B" rows and 1 for the "C" row.</p>
  
  <p>I see a problem that the same datatable is being both set and filtered on, but I tried the following and it didn't work either:</p>
  
  <pre><code>dt &lt;- data.table(category=c("A","A","A","B","B","C"))
  dt2 &lt;- dt
  dt$count &lt;- nrow(dt2[dt2$category == dt$category])
  </code></pre>
  
  <p>In practice, I have a more complex conditional I need to do, but if I could just get this to work I could probably solve the more complex one by myself.</p>
  
  <p>I'm probably pushing filtering too far, but would appreciate any alternative suggestions how to solve this. Thanks.</p>