title: Removing columns from a data.table in R based on conditions
body: <p>How do I remove columns based on values in a data.table in R</p>
  <p>If I have a data.table row,</p>
  <pre><code>dt = data.table(&quot;col1&quot; = &quot;a&quot;, &quot;col2&quot; = &quot;b&quot;, &quot;col3&quot; = &quot;c&quot;, 
  &quot;col4&quot; = 'd', &quot;col5&quot; = &quot;e&quot;, &quot;col6&quot; = 9, &quot;col7&quot; = 0, &quot;col8&quot; = 7,
  &quot;col9&quot; = 0, &quot;col10&quot; = 99)
  </code></pre>
  <p>The first 5 columns are categorical, and the 6-10 columns are numerical. The numbers are repeated for all rows for the numerical columns.</p>
  <p>I had two doubts</p>
  <ol>
  <li><p>How do I remove the columns containing 0s? This column can vary based on inputs - ie sometimes col7 might be 0 sometimes col8 might be 0 etc</p>
  </li>
  <li><p>Once I remove the columns with 0 values, how do I concatenate the rest of the numbers into a single column - in this case, the new column will contain the number 9799</p>
  </li>
  </ol>
  <p>Is there a way to do this without removing the 0 value columns?</p>
  <p>For the first part, I tried</p>
  <pre><code>cols_chosen = c(&quot;col6&quot;, &quot;col7&quot;,&quot;col8&quot;,&quot;col9&quot;,&quot;col10&quot;)
  
  condition = c(FALSE, dt[, lapply(.SD, function(x) sum(x)&lt; 1), .SDcols = cols_chosen])
  
  dt[, which(condition) := NULL]
  </code></pre>
  <p>While I am getting the correct value for the conditions (a list of 5 logical values), the last command is failing with the error</p>
  <pre><code>Error in which(condition) : argument to 'which' is not logical
  </code></pre>
  <p>I had taken the above statements from an earlier answer <a href="https://stackoverflow.com/questions/34902809/remove-columns-of-dataframe-based-on-conditions-in-r/34903938#34903938">Remove columns of dataframe based on conditions in R</a></p>