[R] Trimming a Data Set

Petr Klasterecky klaster at karlin.mff.cuni.cz
Wed Mar 14 19:52:31 CET 2007


If it is precise enough for you, you can use

trim <- function(x,prop=.05) {
trimx <- x[x < quantile(x,prob=(1-prop))]
return(trimx)
}

Petr

Jonathan Morse napsal(a):
> Hi,
> 
> I am trying to restrict a data set so as not to included outliers.  Specifically, I would like to specify a percentage where a fraction of observations are eliminated from the data set, much in the same way that the trimmed mean function works - but leaving the restricted data set intact.
> 
> I have been using a function which will restrict the data set using:
>> trim=function(x,p){
>> o=order(x)
>> xo=x[o]
>> n=length(xo)
>> tl=round(n*p)
>> print(xo[(tl+1):(n-tl)])}
> 
> However I was wondering if anyone knew a more elegant and simple method to get the same result.
> 
> Thanks in advance.
> 
> 
>  
> ---------------------------------
> Don't pick lemons.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list