title: How to match the boolean output of function "by" to input vector
body: <p>I am trying to set data points that fall outside the upper/lower quantiles +/- 3*IQR to NA. The challenge I'm having is how to do this by group of data.</p>
  
  <p>As an example the data set below has a split column and a value column. For each split I need to compute the the upper and lower quantiles and IQRs of the value column, then set the data points in the value column that meets the condition above to NA.</p>
  
  <pre><code>x &lt;- structure(list(Split = c(1L, 1L, 3L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 2L, 3L), Value = c(0.9, 0.9, 3.5, 2.2, 2.2, 2.2, 2.2, 0.9, 3.5, 3.5, 3.5, 1.1, 3.5, 0.9, 1.9, 3.4, 0.9, 3.5, 2.2, 3.5)), .Names = c("Split", "Value"), class = "data.frame", row.names = c(NA, -20L))
  </code></pre>
  
  <p>I have used the "by" function to identify the values that need to be set to NA:</p>
  
  <pre><code>out &lt;- by(
    x$Value, 
    x$Split, 
    function(y)
      y &lt; (quantile(y, probs=c(.25, .75), na.rm = T)[1] - 3*IQR(y, na.rm = T)) |
      y &gt; (quantile(y, probs=c(.25, .75), na.rm = T)[2] + 3*IQR(y, na.rm = T)))
  </code></pre>
  
  <p>The I used the output with "unlist" to set the data points to NA:</p>
  
  <pre><code>x$Value[unlist(out)] &lt;- NA
  </code></pre>
  
  <p>This does not work. Reason is the different sorting between the "by" output and the x$Value column.</p>
  
  <p>Any suggestion on how I can match both outputs and set the corresponding values to NA?</p>
  
  <p>Thanks.</p>