[R] column means dropping column minimum

David L Carlson dcarlson at tamu.edu
Tue Dec 8 20:23:22 CET 2015


The which.min() only gets the first minimum value. If two or more values are tied for the minimum, it will delete only the first one. This would get them all:

> apply(test, 2, function(x) mean(x[-which(x == min(x))]))
   samp1    samp2    samp3 
66.66667 70.00000 73.33333

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Giorgio Garziano
Sent: Tuesday, December 8, 2015 12:58 PM
To: r-help at r-project.org
Subject: Re: [R] column means dropping column minimum

First, your code has flaws in the assignment of NA and in passing na.rm=TRUE to colMeans().

It should be:

test2 <- test
for (i in 1:ncol(test)) { test2[which.min(test[,i]),i]=NA}
print(test2)


samp1 samp2 samp3

1    60    60    NA

2    50    60    65

3    NA    90    65

4    90    NA    90



print(colMeans(test2,na.rm = TRUE))

   samp1    samp2    samp3

66.66667 70.00000 73.33333

For your purpose, I suggest the following:

apply(test, 2, function(x) { mean(x[-which.min(x)])})


GG



	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list