[R] newbie: remove column with low mean from a matrix
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Tue Mar 5 18:10:52 CET 2002
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 remove
What 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list