[R] newbie: remove column with low mean from a matrix

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 5 18:22:21 CET 2002



Tord Snall wrote:
> 
> Dear all,
> 
> Sorry to bother you with one more newbie question.
> 
> I have a dataobject with several hundreds of columns. I want to remove
> columns with a mean of the column values below a certain value:
> 
> > a<- c(1,2,3,4,5,6)
> > b<-c(2,4,6,8,10,12)
> > c<- c(3,6,9,12,15,18)
> > test<- as.matrix(cbind(a, b, c))
> > mean(a)
> [1] 3.5
> > mean(b)
> [1] 7
> > mean(c)
> [1] 10.5
> 
> Say the "certain value" is 5, I would like a new matrix as follows:
> 
> > new.test
>       b  c
> [1,]  2  3
> [2,]  4  6
> [3,]  6  9
> [4,]  8 12
> [5,] 10 15
> [6,] 12 18
> 
> I have searched in the help archive, in help files, and in the
> documentation without finding it.

The following lines should do the trick:

 means <- apply(test, 2, mean)
 test[, means >= 5]

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list