I have a dataset and i want to create an additional column and want to flag the values that are outliers (more than 1.5 times the IQR). I am currently using this code:
#Add additional column for flagging outliers that are beyond 1.5*interquartile range
plotdata$OUTLIERFLAG <- 0
#Cycle through variables
for (i in 1: length(unique(plotdata$variable))){
pms <- unique(plotdata$variable)[i]
dats <- subset(plotdata, plotdata$variable ==pms)
#Cycle through Sampling locations
for (bore in unique(plotdata$Sample.Point)){
subdats <- dats[dats$Sample.Point==bore,]
x1 <- match(boxplot.stats(subdats$value2)$out, subdats$value2)
ifelse(x1==0, NULL, plotdata[rownames(subdats[x1,]),]$OUTLIERFLAG <- 1)
}
}
However, some times the code is not working. for same values, i am getting one flagged as outlier, and the other not. please help