Anthony Dick
2009-Jan-14 22:01 UTC
[R] remove columns containing all zeros (or other value)
Hello- I would like to remove the columns of a matrix that contain all zeros. For example, from x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) I would like to remove the third column. However, because this is in a loop I need a way to first determine which columns are all zeros, and only then remove them. I.e., I don't know which column of x contains all zeros until after x is created. Thanks! Anthony -- Anthony Steven Dick, Ph.D. Post-Doctoral Fellow Human Neuroscience Laboratory Department of Neurology The University of Chicago 5841 S. Maryland Ave. MC-2030 Chicago, IL 60637 Phone: (773)-834-7770 Email: adick at uchicago.edu Web: http://home.uchicago.edu/~adick/
Jorge Ivan Velez
2009-Jan-14 22:12 UTC
[R] remove columns containing all zeros (or other value)
Hi Anthony, Try this: x[,apply(x,2,function(x) !all(x==0))] HTH, Jorge On Wed, Jan 14, 2009 at 5:01 PM, Anthony Dick <adick@uchicago.edu> wrote:> Hello- > > I would like to remove the columns of a matrix that contain all zeros. For > example, from > x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) > > I would like to remove the third column. However, because this is in a loop > I need a way to first determine which columns are all zeros, and only then > remove them. I.e., I don't know which column of x contains all zeros until > after x is created. > > Thanks! > > Anthony > > -- > Anthony Steven Dick, Ph.D. > Post-Doctoral Fellow > Human Neuroscience Laboratory > Department of Neurology > The University of Chicago > 5841 S. Maryland Ave. MC-2030 > Chicago, IL 60637 > Phone: (773)-834-7770 > Email: adick@uchicago.edu > Web: http://home.uchicago.edu/~adick/ > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Gustavo Carvalho
2009-Jan-14 22:22 UTC
[R] remove columns containing all zeros (or other value)
You can also try this: x[,-(which(colSums(x) == 0))] Cheers, Gustavo. On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick <adick at uchicago.edu> wrote:> Hello- > > I would like to remove the columns of a matrix that contain all zeros. For > example, from > x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) > > I would like to remove the third column. However, because this is in a loop > I need a way to first determine which columns are all zeros, and only then > remove them. I.e., I don't know which column of x contains all zeros until > after x is created. > > Thanks! > > Anthony > > -- > Anthony Steven Dick, Ph.D. > Post-Doctoral Fellow > Human Neuroscience Laboratory > Department of Neurology > The University of Chicago > 5841 S. Maryland Ave. MC-2030 > Chicago, IL 60637 > Phone: (773)-834-7770 > Email: adick at uchicago.edu > Web: http://home.uchicago.edu/~adick/ > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
How about: remove.constant.values<-function(x,MARGIN,value2remove) { is.constant.line<-function(x,value2remove) { return(any(x!=value2remove)) } return(unlist(apply(x,MARGIN,is.constant.line,value2remove))) } x[,remove.constant.values(x,2,0)] Jim