Gracefully removing observations with outliers in N fields I have a function.

remove_outliers &lt;- function(x, na.rm = TRUE, ...) {

    #find position of 1st and 3rd quantile not including NA's
    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
    x&lt;-y

    #get rid of any NA's
    x[!is.na(x)]
}


Given a dataset(numbers) like this:

  x
  5
  9
  2
  99
  3
  4


The functioning is obvious

remove_outliers(numbers)


means I now have this:

  x
  5
  9
  2
  3
  4


However, what if I have an ID that I want to retain, such as:

number_id    numbers
12              5
23              9
34              2
45              99
56              3
67              4


How do I remove the outlier(99) with the remove_outliers function(or another, better suited function), to get this data:

number_id    numbers
12              5
23              9
34              2
56              3
67              4


(note the entire observation with the outlier has been removed)

And how can I scale this solution to handle n more variables?

I can do it very ungracefully by taking out each column separately and building a new data frame with loops, but it's hardly readable and a mess to debug. Is there a more graceful way?
This will achieve what you want. You can remove outliers from any column you wish, just pass that column number as an argument in the function.  

id &lt;- c(12,23,34,45,56,67)
num &lt;- c(5,9,2,99,3,4)
prac &lt;- data.frame(id, num)


remove_outliers &lt;- function(x, col) {

  #find position of 1st and 3rd quantile not including NA's
  qnt &lt;- quantile(x[ ,col], probs=c(.25, .75), na.rm = TRUE)

  H &lt;- 1.5 * IQR(x[ ,col])

  x[ ,col] &lt;- ifelse(x[ ,col] &lt; (qnt[1] - H) | x[ ,col] &gt; (qnt[2] + H), NA, x[ ,col])


  #get rid of any NA's
  x &lt;- x[!is.na(x[ ,col]), ]
  x &lt;- assign("dataset", x, envir = .GlobalEnv)
  return(x)
}

remove_outliers(prac, 2)