Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an interesting problem - I'm curious to see other thoughts on this. Looking at the source for <code>table()</code> reveals that it builds off of <code>tabulate()</code>. <code>tabulate()</code> has a few quirks apparently, namely that it only deals with positive integers and returns an integer vector without names. We can use <code>unique()</code> on our vector to apply the <code>names()</code>. If you need to tabulate zero or negative values, I guess going back and reviewing <code>table()</code> would be necessary as <code>tabulate()</code> doesn't seem to do that per the examples on the help page.</p> <pre><code>table2 &lt;- function(data) { x &lt;- tabulate(data) y &lt;- sort(unique(data)) names(x) &lt;- y return(x) } </code></pre> <p>And a quick test:</p> <pre><code>&gt; set.seed(42) &gt; sampData &lt;- sample(1:5, 10000000, TRUE, prob = c(.3,.25, .2, .15, .1)) &gt; &gt; system.time(table(sampData)) user system elapsed 4.869 0.669 5.503 &gt; system.time(table2(sampData)) user system elapsed 0.410 0.200 0.605 &gt; &gt; table(sampData) sampData 1 2 3 4 5 2999200 2500232 1998652 1500396 1001520 &gt; table2(sampData) 1 2 3 4 5 2999200 2500232 1998652 1500396 1001520 </code></pre> <p>EDIT: I just realized there is a <code>count()</code> function in <code>plyr</code> which is another alternative to <code>table()</code>. In the test above, it performs better than <code>table()</code>, and slightly worse than the hack-job solution I put together:</p> <pre><code>library(plyr) system.time(count(sampData)) user system elapsed 1.620 0.870 2.483 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload