title: How to count the number of items in a subset group of an array that are greater than a specific value within that array?
body: <p>I would like to count the number of items in a subset group of an array that are greater than a specific value within that array in R. See below example, for every year there is an external benchmark that is part of the given data (this is not the average of the dataset). For every row where the benchmark of a year is given I would like to add one column with the number of Males with a weight greater than the benchmark; and one column with the number of Females with a weigth greater than the benchmark.</p>
  
  <pre><code>&gt; MyData
     year      type weight DesiredOutput1 DesiredOutput2
  1  1990    Female     78             NA             NA
  2  1990      Male     74             NA             NA
  3  1990    Female     80             NA             NA
  4  1990      Male     90             NA             NA
  5  1990      Male     94             NA             NA
  6  1990      Male     70             NA             NA
  7  1990    Female     65             NA             NA
  8  1990    Female     61             NA             NA
  9  1990 benchmark     78              4              1
  10 1990    Female     71             NA             NA
  11 1990      Male     91             NA             NA
  12 1990    Female     70             NA             NA
  13 1990      Male     81             NA             NA
  14 1991      Male     71             NA             NA
  15 1991 benchmark     79              1              2
  16 1991    Female     80             NA             NA
  17 1991    Female     81             NA             NA
  18 1991      Male     70             NA             NA
  19 1991      Male     80             NA             NA
  20 1991    Female     65             NA             NA
  21 1992    Female     79             NA             NA
  22 1992 benchmark     80              3              1
  23 1992      Male     81             NA             NA
  24 1992      Male     82             NA             NA
  25 1992      Male     86             NA             NA
  26 1992      Male     80             NA             NA
  27 1992    Female     81             NA             NA
  </code></pre>
  
  <p>I can add a count for the number of Males/Females in a given year with below code:</p>
  
  <pre><code>setDT(MyData)[, Count:=.N, by='year,type']
  </code></pre>
  
  <p>But I don't know how to include the fact that I only want to count the number of Males/Females with a weight greater than the benchmark for the given year. Is there a way to use a reference to this benchmark value? I have seen several solutions when you want to count the number of values that is greater than a fixed number (e.g. greater than 70), but how can you compare with a value within your array?</p>