[R] column means dropping column minimum

Giorgio Garziano giorgio.garziano at ericsson.com
Tue Dec 8 19:57:47 CET 2015


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]]



More information about the R-help mailing list