title: Referencing columns in .SDcols using for loop
body: <p>So what I'm trying to achieve is this : Say I have a data table dt having (say) 4 columns. I want to get unique length of every combination of 2 columns.</p>
  <pre><code>DT &lt;- data.table(a = 1:10, b = c(1,1,1,2,2,3,4,4,5,5), c = letters[1:10], d = c(3,3,5,2,4,2,5,1,1,5))
  
  &gt; DT
      a b c d
  1:  1 1 a 3
  2:  2 1 b 3
  3:  3 1 c 5
  4:  4 2 d 2
  5:  5 2 e 4
  6:  6 3 f 2
  7:  7 4 g 5
  8:  8 4 h 1
  9:  9 5 i 1
  10: 10 5 j 5
  </code></pre>
  <p>I tried the following code :</p>
  <pre><code>cols &lt;- colnames(DT)
  for(i in 1:(length(cols)-1)) {
  for (j in i+1:length(cols)) {
      print(unique(DT[,.SD, .SDcols = c(cols[i],cols[j])]))
       }   
     }
  </code></pre>
  <p>Here, basically 'i' goes from first column to second last whereas 'j' is the combining column with 'i'. So the combinations I get are : ab, ac, ad, bc, bd, cd.</p>
  <p>But it gives me the following error</p>
  <blockquote>
  <p>Error in <code>[.data.table</code>(DT, , .SD, .SDcols = c(cols[i], cols[j])) :
  .SDcols missing at the following indices: [2]</p>
  </blockquote>
  <p>If someone can explain why this is and a way around it, I'll be really grateful. Thanks.</p>