[R] detect and replace outliers by the average

Dr Eberhard W Lisse no@p@m @end|ng |rom ||@@e@NA
Thu Apr 20 21:08:08 CEST 2023


There is at least one outliers package on CRAN.

el
On 20/04/2023 20:43, AbouEl-Makarim Aboueissa wrote:
> Dear All:  *please discard my previous email*
> 
> 
> 
> *Re:* detect and replace outliers by the average
> 
> 
> 
> The dataset, please see attached, contains a group factoring column “
> *factor*” and two columns of data “x1” and “x2” with some NA values. I need
> some help to detect the outliers and replace it and the NAs with the
> average within each level (0,1,2) for each variable “x1” and “x2”.
> 
> 
> 
> I tried the below code, but it did not accomplish what I want to do.
> 
> 
> 
> 
> 
> data<-read.csv("G:/20-Spring_2023/Outliers/data.csv", header=TRUE)
> 
> data
> 
> replace_outlier_with_mean <- function(x) {
> 
>   replace(x, x %in% boxplot.stats(x)$out, mean(x, na.rm=TRUE))  #### ,
> na.rm=TRUE NOT working
> 
> }
> 
> data[] <- lapply(data, replace_outlier_with_mean)
> 
> 
> 
> 
> 
> Thank you all very much for your help in advance.
> 
> 
> 
> 
> 
> with many thanks
> 
> abou
> 
> 
> ______________________
> 
> 
> *AbouEl-Makarim Aboueissa, PhD*
> 
> *Professor, Mathematics and Statistics*
> *Graduate Coordinator*
> 
> *Department of Mathematics and Statistics*
> *University of Southern Maine*



More information about the R-help mailing list