I'm trying to obtain the mean of the middle 95% of the values from each row of a matrix (that is, the highest and lowest 2.5% of values in each row are removed before calculating the mean). I am having all sorts of problems with this; for example the command: apply(matrix1,1,function(x) quantile(c(.05,.90),na.rm=T)) returns the exact same quantile values for each row, which is clearly wrong. But even if the values were right, I'm not sure how I would then translate those quantile values into another apply function to get the mean, since they differ from row to row. I also tried: apply(matrix,1,mean,na.rm=T,trim=.05)) and the trim argument was simply ignored Stumped. Any help appreciated. Thanks. Jim Bouldin, PhD Research Ecologist Department of Plant Sciences, UC Davis Davis CA, 95616 530-554-1740
apply(matrix1, 1, quantile, probs = c(0.05, 0.9), na.rm = TRUE) may be what you are looking for. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Bouldin Sent: Thursday, 8 July 2010 8:52 AM To: R help Subject: [R] quantiles on rows of a matrix I'm trying to obtain the mean of the middle 95% of the values from each row of a matrix (that is, the highest and lowest 2.5% of values in each row are removed before calculating the mean). I am having all sorts of problems with this; for example the command: apply(matrix1,1,function(x) quantile(c(.05,.90),na.rm=T)) returns the exact same quantile values for each row, which is clearly wrong. But even if the values were right, I'm not sure how I would then translate those quantile values into another apply function to get the mean, since they differ from row to row. I also tried: apply(matrix,1,mean,na.rm=T,trim=.05)) and the trim argument was simply ignored Stumped. Any help appreciated. Thanks. Jim Bouldin, PhD Research Ecologist Department of Plant Sciences, UC Davis Davis CA, 95616 530-554-1740 ______________________________________________ R-help at r-project.org 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.
On Jul 7, 2010, at 6:52 PM, Jim Bouldin wrote:> > I'm trying to obtain the mean of the middle 95% of the values from > each row > of a matrix (that is, the highest and lowest 2.5% of values in each > row > are removed before calculating the mean).A winsorized.mean?> I am having all sorts of > problems with this; for example the command: > > apply(matrix1,1,function(x) quantile(c(.05,.90),na.rm=T)) > > returns the exact same quantile values for each row, which is clearly > wrong.You gave quantile the same argument each time. Try: apply(matrix1,1,function(x) quantile( x, probs= c(.05,.90),na.rm=T) ) Or: apply(matrix1, 1, quantile, probs= c(.05,.90), na.rm=T )> But even if the values were right, I'm not sure how I would then > translate those quantile values into another apply function to get the > mean, since they differ from row to row.That would be a problem. There is a path to success but it would so much easier if someone already developed a function, wouldn't it? RSiteSearch("winsorized")> > I also tried: > apply(matrix,1,mean,na.rm=T,trim=.05)) > and the trim argument was simply ignored > > > Stumped. Any help appreciated. Thanks. > > > Jim Bouldin, PhD > Research Ecologist > Department of Plant Sciences, UC Davis > Davis CA, 95616 > 530-554-1740 > > ______________________________________________ > R-help at r-project.org 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.David Winsemius, MD West Hartford, CT
On 2010-07-07 16:52, Jim Bouldin wrote:> > I'm trying to obtain the mean of the middle 95% of the values from each row > of a matrix (that is, the highest and lowest 2.5% of values in each row > are removed before calculating the mean). I am having all sorts of > problems with this; for example the command: > > apply(matrix1,1,function(x) quantile(c(.05,.90),na.rm=T)) > > returns the exact same quantile values for each row, which is clearly > wrong. But even if the values were right, I'm not sure how I would then > translate those quantile values into another apply function to get the > mean, since they differ from row to row. > > I also tried: > apply(matrix,1,mean,na.rm=T,trim=.05)) > and the trim argument was simply ignoredI doubt that the 'trim' argument was ignored. If your matrix is of sufficiently small dimensions, then trim may not have any effect. Try trim=0.4 to see if it makes a difference. (And you would probably want trim=0.025 for your application.) -Peter Ehlers> > > Stumped. Any help appreciated. Thanks. > > > Jim Bouldin, PhD > Research Ecologist > Department of Plant Sciences, UC Davis > Davis CA, 95616 > 530-554-1740 > > ______________________________________________