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

J.R. Lockwood lockwood at rand.org
Tue Mar 5 20:53:59 CET 2002


I'm sure there is a faster way, but

vec<-as.vector(apply(test,2,mean))
new.test<-test[,vec>=5]

should do what you need.

> 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



J.R. Lockwood
412-683-2300 x4941
lockwood at rand.org
http://www.rand.org/methodology/stat/members/lockwood/

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