title: Replace row values in data.table using 'by' and conditions
body: <p>I am trying to replace certain row values in a column according to conditions in another column, within a grouping.</p>
  
  <p><strong>EDIT</strong>: edited to highligh the recursive nature of the problem.</p>
  
  <p>E.g.</p>
  
  <pre><code>DT = data.table(y=rep(c(1,3), each = 3)
              ,v=as.numeric(c(1,2,4,4,5,8))
              ,x=as.numeric(rep(c(9:11),each=2)),key=c("y","v"))
  DT
     y v  x
  1: 1 1  9
  2: 1 2  9
  3: 1 4 10
  4: 3 4 10
  5: 3 5 11
  6: 3 8 11
  </code></pre>
  
  <p>Within each 'y', I then want to replace values of 'x' where 'v' has an observation v+t (e.g. t = 3),  with 2222 (or in reality the results of a function) to following result:</p>
  
  <pre><code>   y v  x
  1: 1 1  9
  2: 1 2  9
  3: 1 4 2222
  4: 3 4 10
  5: 3 5 11
  6: 3 8 2222
  </code></pre>
  
  <p>I have tried the following, but to no avail.</p>
  
  <pre><code>  DT[which((v-3) %in% v), x:= 2222, y][]
  </code></pre>
  
  <p>And it mysteriously (?) results in: </p>
  
  <pre><code>   y v  x
  1: 1 1  9
  2: 1 2  9
  3: 1 4 2222
  4: 3 4 2222
  5: 3 5 2222
  6: 3 8 2222
  </code></pre>
  
  <p>Running:</p>
  
  <pre><code>DT[,print(which((v-3) %in% v)), by =y]
  </code></pre>
  
  <p>Indicates that it does the correct indexing within the groups, but what happens from (or the lack thereof) I don't understand. </p>