Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After installing the data.table package and doing some preliminaries...</p> <pre><code>require(data.table) n0&lt;- 1e5 n &lt;- 1e6 DT &lt;- data.table(A1 = sample(1:n0, n, replace = TRUE),B1 = sample(1:n0, n, replace = TRUE)) </code></pre> <p>this does the trick.</p> <pre><code>setkey(DT,A1) DT[ DT[,.N,by=A1], countC:=N ] </code></pre> <p>When you access a data.table with <code>DT[i,j]</code>, you can select rows with <code>i</code> and do something else with <code>j</code>, just like in data.frames.</p> <p><code>DT[,.N,by=A1]</code> selects all rows (since <code>i</code> is blank) and counts rows for each "A1" using the special variable <code>.N</code>.</p> <p>After setting column "A1" as key for DT, we can pass a data.table -- in this case <code>DT[,.N,by=A1]</code> -- in <code>i</code> to merge back the information in the latter data.table. In <code>j</code>, we create a new column in DT using <code>countC:=N</code>. The three vignettes on <a href="http://cran.r-project.org/web/packages/data.table/" rel="nofollow">data.table's CRAN page</a> are a good place to start learning more about how this works.</p> <p><strong>The question at hand.</strong> Oh, I think I see what the original problem was. Suppose <code>unique(x)=c(1,2,4)</code>. If you try <code>table(x)[x]</code>, you will be trying to access <code>table(x)[1]</code>, <code>table(x)[2]</code> and <code>table(x)[4]</code>. The last one is undefined since the length of the table is only 3. R always returns <code>NA</code> when we access indices greater than the length of a vector. For example, look at <code>(1:3)[4]</code>.</p> <p>In your case, if you are missing any unique values in <code>1:n0</code> that are not at the very top, you will see <code>NA</code>s.</p>
 

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