title: Find and replace values with data.table in R?
body: <p>After reading about benchmarks and speed comparisons of R methods, I am in the process of converting to the speedy <code>data.table</code> package for data manipulation on my large data sets.</p>
  
  <p>I am having trouble with a particular task:</p>
  
  <p>For a certain observed variable, I want to check, for each station, if the absolute lagged difference (with lag 1) is greater than a certain threshold. If it is, I want to replace it with <code>NA</code>, else do nothing.</p>
  
  <p>I can do this for the entire <code>data.table</code> using the <code>set</code> command, but I need to do this operation by station. </p>
  
  <p>Example:</p>
  
  <pre><code># Example data. Assume the columns are ordered by date.
  set.seed(1)
  DT &lt;- data.table(station=sample.int(n=3, size=1e6, replace=TRUE), 
                   wind=rgamma(n=1e6, shape=1.5, rate=1/10),
                   other=rnorm(n=1.6),
                   key="station")
  
  # My attempt
  max_rate &lt;- 35
  set(DT, i=which(c(NA, abs(diff(DT[['wind']]))) &gt; max_rate), 
      j=which(names(DT)=='wind'), value=NA)
  # The results
  summary(DT)
  </code></pre>
  
  <p>The trouble with my implementation is that I need to do this by station, and I do not want to get the lagged difference between the last reading in station 1 and the first reading of station 2.</p>
  
  <p>I tried to use the <code>by=station</code> operator within the <code>[ ]</code>, but I am not sure how to do this.</p>