title: r data.table selecting rows on condition
body: <p>I have a data.table like this</p>
  
  <pre><code>library(data.table)
  cells &lt;- c(100, 1, 1980, 1, 0, 1,1,0,1, 150, 1 , 1980, 1,1,1,0,0,0,99,1,1980,1,1,1,1,0,0,899,1,1980,0,1,0,1,1,1 )
  colname &lt;- c("number","sex", "birthy", "2004","2005", "2006", "2007", "2008", "2009")
  rowname &lt;- c("1","2","3","4")
  y &lt;- matrix(cells, nrow=4, ncol=9, byrow=TRUE, dimnames = list(rowname,colname))
  y &lt;- data.table(y, keep.rownames = TRUE)
  </code></pre>
  
  <p>The value 1 in the column 2004 means that this person is continuous insured during the year 2004. Person being insured for 3 preceding years can be part of the study.
  I need a subset of this data.table containing all observation where the following condition is true:
  2004+2005+2006 = 3 or 2005+2006+2007 = or 2006+2007+...</p>
  
  <p>To solve this I wrote a function and wanted to apply this function with lappy, stepping through all years up until the last possible sequence.
  In reality start year and end year and also the minimal length of sequence can vary</p>
  
  <pre><code>years &lt;- function(year, DTbl) {
    DTbl[sum(year:as.numeric(year+2))==3,]
  }
  
  by &lt;- 2004 # study start
  ey &lt;- 2009 # study end
  len &lt;- 2007 # maximal begin of sequence to be tested
  jahre &lt;-3
  DTpy &lt;- data.table::rbindlist(lapply(by:len, years, DTbl=y))
  </code></pre>
  
  <p>Apparently this does not work, but also this</p>
  
  <pre><code>test &lt;- y[ y[, 2006==1], vnew := "ok"]
  </code></pre>
  
  <p>is never true.</p>
  
  <p>After spending a lot of time looking for some answers here I decided to post this question. I am still learning R and I appreciate your input on this issue. </p>
  
  <p>Thanks
  Alina</p>