-5
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

  • I would recommend switching to `ggplot`, in which case this is a duplicate: https://stackoverflow.com/questions/5677885/ignore-outliers-in-ggplot2-boxplot – Harrison Jones Sep 21 '22 at 15:34
  • 2
    it's not difficult to remove the boxplot outliers (outside the whiskers) but when you re-draw the boxplot for the cleaned data the quartiles and IQR will be recalculated and you may find new outliers for the new boxplot – Eric Sep 21 '22 at 15:38
  • @Eric thank you; is it possible to give me the code that may I use. Because after that i tried to perform independent t-test – Kazi Fayzus Salahin Sep 21 '22 at 16:29
  • @HarrisonJones thank you. But I need to performed independent t-test. Is it possible after that. – Kazi Fayzus Salahin Sep 21 '22 at 16:31
  • Eric's solution should provide you with a new data set that has no outliers based on how "outlier" is defined in a boxplot. – Harrison Jones Sep 21 '22 at 20:03

1 Answers1

0

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
  ) 
Eric
  • 1,381
  • 9
  • 24