walter white
walter white

Reputation: 1

How to remove outliers for variable versus another variable in imported dataset in R?

I was asked to make boxplot of variable SAW for the 2 surgical intervention types defined by HSW and dataset name is mydata, Then i was asked to check if there any outliers in the boxplot and i found outliers but i can't remove them and i tried multiple ways but all goes with failure. could you please help me with that issue?

and that is my boxplot

boxplot(mydata$SAW~mydata$HSW,main="SAW for two surgical")



no_outliers <- subset(mydata, mydata$SAW > (Q1 - 1.5*IQR) & mydata$HSW < (Q3 + 1.5*IQR))

This was my last trial but it gave me error says

Error in surgery$SAW : $ operator is invalid for atomic vectors

Upvotes: 0

Views: 103

Answers (1)

user12256545
user12256545

Reputation: 3032

On way would be to use the boxplot object itself-

old <- boxplot(disp~am,mtcars)
 # old$out has the outlier values stored
 # filter the df using those values
new <- mtcars[!mtcars$disp %in% old$out,]
## new boxplot withut ouliers..
boxplot(disp~am,new)

#also 
rstatix::identify_outliers()



Upvotes: 0

Related Questions