title: Looping over columns in data.table R
body: <p>I am trying to loop over columns in data.table package in R. I have been having trouble trying to get the for loop to accurately input the column when I subset the datatable.</p>
  
  <p>The goal of my task is to get the number of rows for each subset of the datatable when the column condition of "==1" is met.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>
  data &lt;- data.table(va=c(1,0,1), vb=c(1,0,0), vc=c(1,1,1))
  
  
  names &lt;- c("va", "vc")
  
  for (col in names) {
      print(nrow(data[col == 1,]))
      print(col)
  }
  </code></pre>
  
  <p>Here is the output I get</p>
  
  <pre><code>[1] 0
  [1] "va"
  [1] 0
  [1] "vc"
  
  </code></pre>
  
  <p>Is there something I am missing or a better way of doing this?</p>