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.testb 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. Thanks in advance! Yours sincerely, Tord Sn?ll ----------------------------------------------------------------------- Tord Sn?ll Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University Villav?gen 14 SE-752 36 Uppsala, Sweden Tel: 018-471 28 82 (int +46 18 471 28 82) (work) Tel: 018-25 71 33 (int +46 18 25 71 33) (home) Fax: 018-55 34 19 (int +46 18 55 34 19) (work) E-mail: Tord.Snall at ebc.uu.se http://www.vaxtbio.uu.se/resfold/snall.htm ------------------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2002-Mar-05 17:10 UTC
[R] newbie: remove column with low mean from a matrix
On Tue, 5 Mar 2002, Tord Snall wrote:> Sorry to bother you with one more newbie question.A `newbie' of long standing, I believe.> I have a dataobject with several hundreds of columns. I want to removeWhat sort of object? matrix or data frame or private class "dataobject" or ....> 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.Let's first assume a matrix, as you used one mns <- colMeans(test) # apply(test, 2, mean) in 1.4.x test[, mns >= 5, drop = F] For a data frame: mns <- sapply(dfr, mean) dfr[mns >= 5] I know a good book or two about this kind of thing. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 18J.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._