How I do remove the Outlier using R?

Viewed 77
weight<-c(117,  118,    125,    86,     131,     93,    103,    107,    112,    97, 105,    105,    111,    105,    124,    111,    103,    113,    112,    127,    111,    115,    108,    105,    108,    127,    148,    131,    126,    119,    131,    134,    127,    139,    106,    133,    139,    125,    127,    127,    113,    135,    113,    131,    145,    147,    139,    136)

gender<-c(1,    1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2)

data<-data.frame(weight,gender)
attach(data)

boxplot(weight[gender==1], weight[gender==2], names = c("Male", "Female"),
    col = topo.colors(6))

After running this I got one outlier in each category. How I remove this outlier using R I also attach the Image enter image description here of boxplot

1 Answers

try a tidy solution:

library(dplyr)

cleaned_data = data %>%
  group_by(gender) %>%
  mutate(
    lo_whisker = quantile(weight,.25)-IQR(weight)*1.5,
    hi_whisker = quantile(weight,.75)+IQR(weight)*1.5
  ) %>%
  ungroup() %>%
  filter(
    weight >= lo_whisker&weight <= hi_whisker
  ) 
Related