title: Row wise logical operation on Data Table in R
body: <p>I have to perform row wise logical operation in data table, let me explain this with example. Suppose I have data table (DT) as given below</p>
  
  <pre><code>V1      V2        V3        V4      V5 
   AAE     CDG       2        0        0  
   AAE     CDG       2        5        5  
   AAE     ORY       2        4        4  
   AAE     ORY       2        0        0  
   AAE     ORY       2        5        5  
   AAE     ORY       2        3        3  
  </code></pre>
  
  <p>Now, depending upon the value of V5, I want to add additional column V6 to data table, this is logical operation. I did something like this to do it-</p>
  
  <pre><code>DT[, V6 := if(V5 == 0){1
                }else if(V5 == 1){2
                }else if(V5 == 2){3
                }else if(V5 == 3){4
                }else if(V5 == 4){5
                }else if(V5 == 5){6}
      ]
  </code></pre>
  
  <p>But this does not give the desired result, which should be </p>
  
  <pre><code>V1   V2        V3      V4        V5   V6
   AAE CDG       2        0        0     1
   AAE CDG       2        5        5     6
   AAE ORY       2        4        4     5
   AAE ORY       2        0        0     1
   AAE ORY       2        5        5     6
   AAE ORY       2        3        3     4
  </code></pre>
  
  <p>Whereas gives the following result-</p>
  
  <pre><code> V1   V2       V3       V4       V5    V6
   AAE CDG       2        0        0     1
   AAE CDG       2        5        5     1
   AAE ORY       2        4        4     1
   AAE ORY       2        0        0     1
   AAE ORY       2        5        5     1
   AAE ORY       2        3        3     1
  </code></pre>
  
  <p>This happens because first value of V5 (which is 0) is used in logical operation, instead of dynamically using one value or row value at a time. How can I change [,J] argument to get the desired results. I can use the for loop to do this, but it would be very in-efficient way of doing it.</p>