Hi Everbody Could somebody help me.? I need to remove the columns where the sum of it components is equal to zero. For example> a<-matrix(c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0), ncol=4) > a[,1] [,2] [,3] [,4] [1,] 0 0 0 1 [2,] 0 1 0 1 [3,] 0 0 0 0 [4,] 0 1 0 0 [5,] 0 0 0 1 [6,] 0 0 0 0 Columns 1 and 3 should be removed the result should be the dollowing matrix [,2] [,4] [1,] 0 1 [2,] 1 1 [3,] 0 0 [4,] 1 0 [5,] 0 1 [6,] 0 0 Thanks again -- Alberto Lora Michiels Rue du Progrès, 6B 7860 Lessines GSM 32(0)496659457 [[alternative HTML version deleted]]
Hi Alberto, On Aug 18, 2009, at 4:14 AM, Alberto Lora M wrote:> Hi Everbody > > Could somebody help me.? > > I need to remove the columns where the sum of it components is equal > to > zero. > > For example > >> a<-matrix(c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0), ncol=4) >> a > [,1] [,2] [,3] [,4] > [1,] 0 0 0 1 > [2,] 0 1 0 1 > [3,] 0 0 0 0 > [4,] 0 1 0 0 > [5,] 0 0 0 1 > [6,] 0 0 0 0 > > Columns 1 and 3 should be removed > > the result should be the dollowing matrix > > [,2] [,4] > [1,] 0 1 > [2,] 1 1 > [3,] 0 0 > [4,] 1 0 > [5,] 0 1 > [6,] 0 0Try this: R> a[,-which(colSums(a) == 0)] [,1] [,2] [1,] 0 1 [2,] 1 1 [3,] 0 0 [4,] 1 0 [5,] 0 1 [6,] 0 0 Indexing into a matrix/vector/data.frame/list/whatever with a negative number removes those elements from the result. -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Try this, a[ ,as.logical(colSums(a))] mind an unfortunate logical vs integer indexing trap: isTRUE(all.equal( a[ ,!!colSums(a)] , a[ ,colSums(a)] )) [1] FALSE HTH, baptiste 2009/8/18 Alberto Lora M <albertoloram at gmail.com>:> Hi Everbody > > Could somebody help me.? > > I need to remove the columns where the sum of it components is equal to > zero. > > For example > >> a<-matrix(c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0), ncol=4) >> a > ? ? [,1] [,2] [,3] [,4] > [1,] ? ?0 ? ?0 ? ?0 ? ?1 > [2,] ? ?0 ? ?1 ? ?0 ? ?1 > [3,] ? ?0 ? ?0 ? ?0 ? ?0 > [4,] ? ?0 ? ?1 ? ?0 ? ?0 > [5,] ? ?0 ? ?0 ? ?0 ? ?1 > [6,] ? ?0 ? ?0 ? ?0 ? ?0 > > Columns 1 and 3 should be removed > > the result should be the dollowing matrix > > ? ? [,2] ?[,4] > [1,] ? ?0 ? ?1 > [2,] ? ?1 ? ?1 > [3,] ? ?0 ? ?0 > [4,] ? ?1 ? ?0 > [5,] ? ?0 ? ?1 > [6,] ? ?0 ? ?0 > > Thanks again > > > -- > Alberto Lora Michiels > Rue du Progr?s, ?6B > 7860 Lessines > GSM 32(0)496659457 > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK http://newton.ex.ac.uk/research/emag
Hi, This line of code does the trick: a[,which(apply(a, 2, sum) != 0)] cheers, Paul Alberto Lora M wrote:> Hi Everbody > > Could somebody help me.? > > I need to remove the columns where the sum of it components is equal to > zero. > > For example > > >> a<-matrix(c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0), ncol=4) >> a >> > [,1] [,2] [,3] [,4] > [1,] 0 0 0 1 > [2,] 0 1 0 1 > [3,] 0 0 0 0 > [4,] 0 1 0 0 > [5,] 0 0 0 1 > [6,] 0 0 0 0 > > Columns 1 and 3 should be removed > > the result should be the dollowing matrix > > [,2] [,4] > [1,] 0 1 > [2,] 1 1 > [3,] 0 0 > [4,] 1 0 > [5,] 0 1 > [6,] 0 0 > > Thanks again > > > > ------------------------------------------------------------------------ > > ______________________________________________ > 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. >-- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://intamap.geo.uu.nl/~paul
Hi r-help-bounces at r-project.org napsal dne 18.08.2009 10:14:26:> Hi Everbody > > Could somebody help me.? > > I need to remove the columns where the sum of it components is equal to > zero. > > For example > > > a<-matrix(c(0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0), ncol=4) > > a > [,1] [,2] [,3] [,4] > [1,] 0 0 0 1 > [2,] 0 1 0 1 > [3,] 0 0 0 0 > [4,] 0 1 0 0 > [5,] 0 0 0 1 > [6,] 0 0 0 0 > > Columns 1 and 3 should be removed > > the result should be the dollowing matrix > > [,2] [,4] > [1,] 0 1 > [2,] 1 1 > [3,] 0 0 > [4,] 1 0 > [5,] 0 1 > [6,] 0 0a[,!colSums(a)==0] Beware of "==" and finite precision of floating point numbers (see FAQ) Regards Petr> > Thanks again > > > -- > Alberto Lora Michiels > Rue du Progr?s, 6B > 7860 Lessines > GSM 32(0)496659457 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.