I'm using this code to find the outliers in the data frame.
out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)
The first columns are just the names (that's why I'm skipping it). I've found this method to detect the specific position of the outliers.
out_ind <- which(df$col1 %in% c(out))
But in my case, the object "out" is not a single boxplot.stats(x)$out so I cannot run the which formula. How can I write it to find the outliers' position in every single data frame column?
CodePudding user response:
You now have a list, so you need to iterate over it, e.g. using the apply
functions:
df <- mtcars
out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)
out_ind <- sapply(names(df[,-1]), \(col) which(
df[[col]] %in% out[[col]]
)
)
# out_ind
# $cyl
# integer(0)
# $disp
# integer(0)
# $hp
# [1] 31
# $drat
# integer(0)
# $wt
# [1] 16 17
# $qsec
# [1] 9
# $vs
# integer(0)
# $am
# integer(0)
# $gear
# integer(0)
# $carb
# [1] 31