Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are going to be doing complicated operations inside your <code>j</code> expressions, you should probably use <code>eval</code> and <code>quote</code>. One problem with that in current version of <code>data.table</code> is that the environment of <code>eval</code> is not always correctly processed - <a href="https://stackoverflow.com/questions/15913832/eval-and-quote-in-data-table">eval and quote in data.table</a> (Note: There has been an update to that answer based on an update to the package.) - and the current fix for that is to add <code>.SD</code> to <code>eval</code>. As far as I can tell from a few tests that I've run this doesn't affect speed (the way e.g. having <code>.SD[1]</code> in <code>j</code> would).</p> <p>Interestingly this issue only plagues the <code>j</code> and you'll be fine using <code>eval</code> normally in <code>i</code> (where <code>.SD</code> is not available anyway).</p> <p>The other problem is assignment, and there you have to have strings. I know one way to extract the string name from a quoted expression - it's not pretty, but it works. Here's an example combining everything together:</p> <pre><code>x = data.table(dist = c(1:10), val = c(1:10)) distcol = quote(dist) valcol = quote(val) x[eval(valcol) &lt; 5, capture.output(str(distcol, give.head = F)) := eval(distcol)*sum(eval(distcol, .SD))] </code></pre> <p>Note how I was ok not adding <code>.SD</code> in one <code>eval(distcol)</code>, but won't be if I take it out of the other <code>eval</code>.</p> <p>Another option is to use <code>get</code>:</p> <pre><code>diststr = "dist" valstr = "val" x[get(valstr) &lt; 5, c(diststr) := get(diststr)*sum(get(diststr))] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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