title: Product Combination of variables by loop in R with data table
body: <p>I have an initial table in R including 7 variables as shown below:</p>
  <pre><code>library(data.table)
  Data&lt;-data.frame(
    ID=c(1,1,1,2,2,2,3,3,3,4,4,4),
    CP1 =c(1,0,0,1,0,0,1,0,0,1,0,0),
    CP2 =c(0,1,1,0,0,1,0,1,0,0,0,0),
    CP3 =c(0,0,0,0,0,0,0,0,0,0,0,1),
    PR1 =c(1,1,0,0,0,0,0,0,0,0,0,0),
    PR2=c(0,0,1,0,0,0,0,0,0,0,0,0),
    PR3=c(0,0,0,0,1,0,0,0,0,0,0,1)
    
    )
  Data
  
  &gt; Data
     ID CP1 CP2 CP3 PR1 PR2 PR3
  1   1   1   0   0   1   0   0
  2   1   0   1   0   1   0   0
  3   1   0   1   0   0   1   0
  4   2   1   0   0   0   0   0
  5   2   0   0   0   0   0   1
  6   2   0   1   0   0   0   0
  7   3   1   0   0   0   0   0
  8   3   0   1   0   0   0   0
  9   3   0   0   0   0   0   0
  10  4   1   0   0   0   0   0
  11  4   0   0   0   0   0   0
  12  4   0   0   1   0   0   1
  </code></pre>
  <p>I want to create all the product combination of CP1, CP2 and CP3 with the PR1, PR2 and PR3 variables, with the following names CP1_PR1, CP1_PR2, CP1_PR3, CP2_PR1, CP2_PR2, CP2_PR3, CP3_PR1, CP3_PR2 and CP3_PR3.</p>
  <p><em><strong>But I want to use a condition for doing this product. When both CP and PR variable are equal to 1 I want to create the CP_PR variable which will be equal to 1 and also to make zero the initial CP variable.</strong></em></p>
  <p>I make a vector with the names of the CP variables and a vector of the PR variables:</p>
  <pre><code>ListCP&lt;-colnames(Data)[2:4]
  ListPr&lt;-colnames(Data)[5:7]
  </code></pre>
  <p>And then I use a double for loop in order to create the needed product combination Variables which correct creates the combinations I want:</p>
  <pre><code>for (i in ListPr) {
    
    for (j in ListCP) {
      
      Data&lt;-Data[,paste0(j,&quot;_&quot;,i) := ifelse(get(i)==1 &amp; get(j)==1,1,0)]
  
      
    }
    
  }
  
  &gt; Data
  &gt; Data
      ID CP1 CP2 CP3 Pr1 Pr2 Pr3 CP1_Pr1 CP2_Pr1 CP3_Pr1 CP1_Pr2 CP2_Pr2 CP3_Pr2 CP1_Pr3 CP2_Pr3 CP3_Pr3
   1:  1   1   0   0   1   0   0       1       0       0       0       0       0       0       0       0
   2:  1   0   1   0   1   0   0       0       1       0       0       0       0       0       0       0
   3:  1   0   1   0   0   1   0       0       0       0       0       1       0       0       0       0
   4:  2   1   0   0   0   0   0       0       0       0       0       0       0       0       0       0
   5:  2   0   0   0   0   0   1       0       0       0       0       0       0       0       0       0
   6:  2   0   1   0   0   0   0       0       0       0       0       0       0       0       0       0
   7:  3   1   0   0   0   0   0       0       0       0       0       0       0       0       0       0
   8:  3   0   1   0   0   0   0       0       0       0       0       0       0       0       0       0
   9:  3   0   0   0   0   0   0       0       0       0       0       0       0       0       0       0
  10:  4   1   0   0   0   0   0       0       0       0       0       0       0       0       0       0
  11:  4   0   0   0   0   0   0       0       0       0       0       0       0       0       0       0
  12:  4   0   0   1   0   0   1       0       0       0       0       0       0       0       0       1
  </code></pre>
  <p>Then when I am trying to make zero the initial CP vars in case that I said above I am getting an error.</p>
  <pre><code>&gt; for (i in ListCP) {
  +   
  +   for (j in ListPr) {
  +     
  +     Data&lt;-Data[paste0(j,&quot;_&quot;,i)==1,.(j) := 0]
  +     
  +     
  +   }
  +   
  + }
  Error in `[.data.table`(Data, paste0(j, &quot;_&quot;, i) == 1, `:=`(.(j), 0)) : 
    LHS of := must be a symbol, or an atomic vector (column names or positions).
  </code></pre>
  <p>My question here is if I can combine both double for loops into one without getting an error.
  Also, because my dataset is bigger, any other faster implementation would be greatly appreciated.</p>
  <p>Thank you.</p>