title: Add missing rows to data.table according to multiple keyed columns
body: <p>I have a <code>data.table</code> object that contains multiple columns that specify unique cases.  In the small example below, the variables "<code>name</code>", "<code>job</code>", and "<code>sex</code>" specify the unique IDs.  I would like to add missing rows so that each each case has a row for each possible instance of another variable, "<code>from</code>" (similar to <code>expand.grid</code>).</p>
  
  <pre><code>library(data.table)
  set.seed(1)
  mydata &lt;- data.table(name = c("john","john","john","john","mary","chris","chris","chris"),
                   job = c("teacher","teacher","teacher","teacher","police","lawyer","lawyer","doctor"),
                   sex = c("male","male","male","male","female","female","male","male"),
                   from = c("NYT","USAT","BG","TIME","USAT","BG","NYT","NYT"),
                   score = rnorm(8))
  
  setkeyv(mydata, cols=c("name","job","sex"))
  
  mydata[CJ(unique(name, job, sex), unique(from))]
  </code></pre>
  
  <p>Here's the current data.table object:</p>
  
  <pre><code>&gt; mydata
      name     job    sex from      score
  1:  john teacher   male  NYT -0.6264538
  2:  john teacher   male USAT  0.1836433
  3:  john teacher   male   BG -0.8356286
  4:  john teacher   male TIME  1.5952808
  5:  mary  police female USAT  0.3295078
  6: chris  lawyer female   BG -0.8204684
  7: chris  lawyer   male  NYT  0.4874291
  8: chris  doctor   male  NYT  0.7383247
  </code></pre>
  
  <p>Here's the result I'd like:</p>
  
  <pre><code>&gt; mydata
       name     job    sex from      score
  1:   john teacher   male  NYT -0.6264538
  2:   john teacher   male USAT  0.1836433
  3:   john teacher   male   BG -0.8356286
  4:   john teacher   male TIME  1.5952808
  5:   mary  police female  NYT  NA
  6:   mary  police female USAT  0.3295078
  7:   mary  police female   BG  NA
  8:   mary  police female TIME  NA
  9:  chris  lawyer female  NYT -NA
  10: chris  lawyer female USAT -NA
  11: chris  lawyer female   BG -0.8204684
  12: chris  lawyer female TIME -NA
  13: chris  lawyer   male  NYT  0.4874291
  14: chris  lawyer   male USAT  NA
  15: chris  lawyer   male   BG  NA
  16: chris  lawyer   male TIME  NA
  17: chris  doctor   male  NYT  0.7383247
  18: chris  doctor   male USAT  NA
  19: chris  doctor   male   BG  NA
  20: chris  doctor   male TIME  NA
  </code></pre>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>setkeyv(mydata, cols=c("name","job","sex"))
  mydata[CJ(unique(name, job, sex), unique(from))]
  </code></pre>
  
  <p>But I receive the following error and adding fromLast=TRUE (or FALSE) does not give me the right solution:</p>
  
  <pre><code>Error in unique.default(name, job, sex) : 
    'fromLast' must be TRUE or FALSE
  </code></pre>
  
  <p>Here are the relevant answers I've come across (but none appears to deal with multiple keyed columns):
  <a href="https://stackoverflow.com/questions/22462405/add-missing-rows-to-a-data-table">add missing rows to a data table</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16518505/efficiently-inserting-default-missing-rows-in-a-data-table">Efficiently inserting default missing rows in a data.table</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10438969/fastest-way-to-add-rows-for-missing-values-in-a-data-frame">Fastest way to add rows for missing values in a data.frame?</a></p>