title: Using R aggregate() function, how do I count distinct values?
body: <p>My data look like this:</p>
  <pre><code>str(defects)
  
  ## 'data.frame':    22540 obs. of  8 variables:
  ##  $ BUG_ID         : int  2237 2239 2163 2163 2163 2163 2163 2163 2163 2163 ...
  ##  $ STATUS         : Factor w/ 5 levels &quot;Assigned&quot;,&quot;Closed&quot;,..: 2 2 2 2 2 2 2 2 2 2 ...
  ##  $ SEVERITY       : Factor w/ 4 levels &quot;1-Low&quot;,&quot;2-Medium&quot;,..: 4 3 3 3 3 3 3 3 3 3 ...
  ##  $ DETECTION_DATE : Date, format: &quot;2017-10-31&quot; &quot;2017-10-31&quot; ...
  ##  $ ACTUAL_FIX_TIME: int  1 1 20 20 20 20 20 20 20 20 ...
  ##  $ CLOSING_DATE   : Date, format: &quot;2017-10-31&quot; &quot;2017-10-31&quot; ...
  ##  $ DATE           : Date, format: &quot;2017-10-31&quot; &quot;2017-10-31&quot; ...
  ##  $ NOR            : int  1 1 1 1 1 1 1 1 1 1 ...
  </code></pre>
  <p><strong>I need to calculate the following using the aggregate function:</strong></p>
  <pre><code>COUNT_DISTINCT(
      IF [CLOSING_DATE] == [DATE] THEN
          [BUG_ID] 
      END  
  )
  </code></pre>
  <p>Here is what I have:</p>
  <pre><code>aggregate(unique(BUG_ID) ~ DATE, defects, subset = CLOSING_DATE == DATE, length)
  </code></pre>