title: Using `:=` in data.table to sum the values of two columns in R, ignoring NAs
body: <p>I have what I think is a very simple question related to the use of data.table and the <code>:=</code> function. I don't think I quite understand the behaviour of <code>:=</code> and often I run into similar problems.</p>
  
  <p>Here is some example data</p>
  
  <pre><code> mat &lt;- structure(list(
                col1 = c(NA, 0, -0.015038, 0.003817, -0.011407), 
                col2 = c(0.003745, 0.007463, -0.007407, -0.003731, -0.007491)), 
                .Names = c("col1", "col2"), 
                row.names = c(NA, 10L), 
                class = c("data.table", "data.frame"))
  </code></pre>
  
  <p>which gives</p>
  
  <pre><code>&gt; mat
           col1      col2
   1:        NA  0.003745
   2:  0.000000  0.007463
   3: -0.015038 -0.007407
   4:  0.003817 -0.003731
   5: -0.011407 -0.007491
  </code></pre>
  
  <p>I want to create a column called col3 which gives the sum of col1 and col2. If I use</p>
  
  <pre><code>mat[,col3 := col1 + col2]
  
  #        col1      col2      col3
  #1:        NA  0.003745        NA
  #2:  0.000000  0.007463  0.007463
  #3: -0.015038 -0.007407 -0.022445
  #4:  0.003817 -0.003731  0.000086
  #5: -0.011407 -0.007491 -0.018898
  </code></pre>
  
  <p>then I get an NA for the first row, but I want NAs to be ignored. So I tried instead</p>
  
  <pre><code>mat[,col3 := sum(col1,col2,na.rm=TRUE)]
  
  #        col1      col2      col3
  #1:        NA  0.003745 -0.030049
  #2:  0.000000  0.007463 -0.030049
  #3: -0.015038 -0.007407 -0.030049
  #4:  0.003817 -0.003731 -0.030049
  #5: -0.011407 -0.007491 -0.030049
  </code></pre>
  
  <p>which is not what I am after, since it is giving me the sum of all elements of col1 and col2. I think I don't quite get <code>:=</code>... How can I get the sum of the element of col1 and col2 ignoring NA values?</p>
  
  <p>Not sure this is relevant, but here is my sessionInfo</p>
  
  <pre><code>&gt; sessionInfo()
  R version 2.15.1 (2012-06-22)
  Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
  
  locale:
  [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] data.table_1.8.3
  </code></pre>