[R] Remove columns

baptiste auguie baptiste.auguie at googlemail.com
Tue Aug 18 16:49:24 CEST 2009


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




More information about the R-help mailing list