title: count unique values for each column per ID in R
body: <p>I have a dataset which is quite big (140000 obs * 125 attributes). Each ob is associated with an ID (which can be unique or not). I want to count unique values for each attribute (columns) per ID. </p>
  
  <p>I tried <code>aggregate(. ~ ID, mydata, function(x) length(unique(x))</code>. But it doesn't work. Also given the size of the data frame, I feel even it works it may take too long to do it. Anyone knows a better way to do it?</p>
  
  <p>The dataset: </p>
  
  <pre><code>ID  Attr1   Attr2   Attr3   Attr125
  1   A   X   Y   123
  1   B   Z   Y   345
  1   B   X   Y   134
  2   A   Z   Y   abc
  2   C   Y   Y   def
  3   D   Y   N   xyz
  4   B   Z   Y   789
  </code></pre>
  
  <p>The result I want: </p>
  
  <pre><code>ID  Attr1   Attr2   Attr3   Attr125
  1   2   2   1   3
  2   2   2   1   2
  3   1   1   1   1
  4   1   1   1   1
  </code></pre>