简体   繁体   中英

Finding the mean of a vector while omitting values greater than a constant value in Rstudio

I have a data set, which I need to find the mean. However, there are some values that are errors in the experiment and I want to omit them from the data set before I find the mean.

I have the data set as a vector.

Is there a way to omit the values while taking the mean, or maybe add the values in the vector A that are less than the constant to another vector B and find the mean of vector B?

Using RStudio

The 'outliers' package might be useful for your problem; without more information it's difficult to help you.

install.packages("outliers")
library(outliers)
vec <- c(44,55,45,46,47,10)
#[1] 44 55 45 46 47 10
mean(vec)
#[1] 41.16667
vec[!vec %in% outlier(vec)]
#[1] 44 55 45 46 47
mean(vec[!vec %in% outlier(vec)])
#[1] 47.4

-- If you know what the cutoff is supposed to be i.e. >20 --
mean(vec[vec >= 20])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM