title: Function to remove outliers by group from dataframe
body: <p>I am trying to remove the outliers from my dataframe containing <code>x</code> and <code>y</code> variables grouped by variable <code>cond</code>. </p>
  
  <p>I have created a function to remove the outliers based on a boxplot statistics, and returning <code>df</code> without outliers. The function works well when applied for a raw data. However, if applied on grouped data, the function does not work and I got back an error: </p>
  
  <pre><code>Error in mutate_impl(.data, dots) : 
    Evaluation error: argument "df" is missing, with no default.
  </code></pre>
  
  <p>Please, how can I correct my function to take vectors <code>df$x</code> and <code>df$y</code> as arguments, and correctly get rid of outliers by group?</p>
  
  <p><a href="https://i.stack.imgur.com/UeirD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UeirD.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>My dummy data:</p>
  
  <pre><code>set.seed(955)
  # Make some noisily increasing data
  dat &lt;- data.frame(cond = rep(c("A", "B"), each = 22),
                    xvar = c(1:10+rnorm(20,sd=3), 40, 10, 11:20+rnorm(20,sd=3), 85, 115),
                    yvar = c(1:10+rnorm(20,sd=3), 200, 60, 11:20+rnorm(20,sd=3), 35, 200))
  
  
  removeOutliers&lt;-function(df, ...) {
  
    # first, identify the outliers and store them in a vector
    outliers.x&lt;-boxplot.stats(df$x)$out
    outliers.y&lt;-boxplot.stats(df$y)$out
  
    # remove the outliers from the original data
    df&lt;-df[-which(df$x %in% outliers.x),]
    df[-which(df$y %in% outliers.y),]
  }
  
  # REmove outliers (try if function works)
  removeOutliers(dat)
  
  # Apply the function to group
  # Not working!!!
  
  dat_noOutliers&lt;- dat %&gt;%
    group_by(cond) %&gt;%
    mutate(removeOutliers)
  </code></pre>
  
  <hr>
  
  <p>I have found this function to remove the outliers from a vector data . However, I would like to remove outliers from both <code>df$x</code> and <code>df$y</code> vectors in a dataframe.</p>
  
  <pre><code>remove_outliers &lt;- function(x, na.rm = TRUE, ...) {
    qnt &lt;- quantile(x, probs=c(.25, .75), na.rm = na.rm, ...)
    H &lt;- 1.5 * IQR(x, na.rm = na.rm)
    y &lt;- x
    y[x &lt; (qnt[1] - H)] &lt;- NA
    y[x &gt; (qnt[2] + H)] &lt;- NA
    y
  }
  </code></pre>
  
  <p>(<a href="https://stackoverflow.com/questions/49982794/remove-outliers-by-group-in-r">remove outliers by group in R</a>)</p>