title: data.table apply function on each column
body: <p>I guess I am pretty close to a solution, but I struggle to combine lapply with data.table. I read a lot about lapply and find some example with data.table, but the way of thinking is new to me and its driving me nuts...</p>
  
  <p>This is my data.table</p>
  
  <pre><code>cells &lt;- c(150, 1,1980,1,1,1,0,0,0,1,2004,3,
         99 , 1,1980,1,1,1,1,0,0,0,2004,4,
         899, 1,1980,0,1,0,1,1,1,1,2007,4,
         789, 1,1982,1,1,1,0,1,1,1,2004,3 )
  colname &lt;- c("number","sex", "birthy", "2004","2005", "2006", "2007", "2008", "2009","2010","begy","SeqLen")
  rowname &lt;- c("2","3","4","5")
  y &lt;- matrix(cells, nrow=4, ncol=12, byrow=TRUE, dimnames = list(rowname,colname))
  y &lt;- data.table(y, keep.rownames = TRUE)
  </code></pre>
  
  <p>I want to step through a vector of column names</p>
  
  <pre><code>cols &lt;- c(paste(2004:2010, sep=" "))
  </code></pre>
  
  <p>Doing the following operation on just one column works fine!</p>
  
  <pre><code>vec &lt;- "2005"
  y[,  (vec) := ifelse((vec) &lt; as.numeric(begy),0, ifelse( ((vec) &gt; as.numeric(begy) + as.numeric(SeqLen) -1) ,0,1)) ]
  </code></pre>
  
  <p>Creating a function and stepping through the vector seams a good solution, but how? 
  I found this... </p>
  
  <pre><code>dt[ , (cols) := lapply(.SD, "*", -1), .SDcols = cols]
  </code></pre>
  
  <p>but where can I put my ifelse in this example?</p>
  
  <p>I also read about for-loop and set function, like this...</p>
  
  <pre><code>for (j in cols) set(dt, j = j, value = ifelse((dt[[j]]) &lt; as.numeric(dt[[begy]]),0, ifelse( (dt[[j]] &gt; as.numeric(dt[[begy]]) + as.numeric(dt[[SeqLen]]) -1) ,0,1)))
  </code></pre>
  
  <p>but this is nonsense.</p>
  
  <p>Thanks
  Alina</p>