I'm looking for an efficient way of removing zero-variance columns from a large matrix. Any suggestions? Thanks, - Moises [[alternative HTML version deleted]]
"Moises Hassan" <mhassan at scitegic.com> writes:> I'm looking for an efficient way of removing zero-variance columns from > a large matrix. > > Any suggestions?A[,apply(A,2,var)>0] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Something like: keep <- apply(myData, 2, function(x) diff(range(x)) > 0) newData <- myData[, keep] Andy> From: Moises Hassan > > I'm looking for an efficient way of removing zero-variance > columns from > a large matrix. > > Any suggestions? > > Thanks, > > - Moises >
It works great, except that in the case where only one column is left, it returns a vector and the column name is lost. How can you avoid that behavior? Thanks, - Moises -----Original Message----- From: Spencer Graves [mailto:spencer.graves at pdf.com] Sent: Saturday, August 28, 2004 8:26 PM To: Peter Dalgaard Cc: Moises Hassan; R Help Subject: Re: [R] removing invariant columns from a matrix Both the previous solutions seem to assume a numeric matrix. How about the following: A <- array(letters[c(rep(1, 13), rep(2, 13), 1:26)], dim=c(13, 5)) A[, apply(A, 2, function(x)any(x[-1] != x[-length(x)]))] A[, apply(A, 2, function(x)any(x[-1] != x[-length(x)])] enjoy. spencer graves Peter Dalgaard wrote:>"Moises Hassan" <mhassan at scitegic.com> writes: > > > >>I'm looking for an efficient way of removing zero-variance columnsfrom>>a large matrix. >> >>Any suggestions? >> >> > >A[,apply(A,2,var)>0] > > >
Thanks, that solved the problem! - Moises -----Original Message----- From: Roger D. Peng [mailto:rpeng at jhsph.edu] Sent: Sunday, August 29, 2004 3:08 PM To: Moises Hassan Cc: R Help Subject: Re: [R] removing invariant columns from a matrix See question 7.7 in the R FAQ (http://cran.r-project.org/doc/FAQ/R-FAQ.html) -roger Moises Hassan wrote:> It works great, except that in the case where only one column is left, > it returns a vector and the column name is lost. How can you avoidthat> behavior? > > Thanks, > - Moises > > > -----Original Message----- > From: Spencer Graves [mailto:spencer.graves at pdf.com] > Sent: Saturday, August 28, 2004 8:26 PM > To: Peter Dalgaard > Cc: Moises Hassan; R Help > Subject: Re: [R] removing invariant columns from a matrix > > Both the previous solutions seem to assume a numeric matrix.How> about the following: > > A <- array(letters[c(rep(1, 13), rep(2, 13), 1:26)], dim=c(13, 5)) > A[, apply(A, 2, function(x)any(x[-1] != x[-length(x)]))] > A[, apply(A, 2, function(x)any(x[-1] != x[-length(x)])] > > enjoy. spencer graves > > Peter Dalgaard wrote: > > >>"Moises Hassan" <mhassan at scitegic.com> writes: >> >> >> >> >>>I'm looking for an efficient way of removing zero-variance columns > > from > >>>a large matrix. >>> >>>Any suggestions? >>> >>> >> >>A[,apply(A,2,var)>0] >> >> >> > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>