[R] Remove columns

Paul Hiemstra p.hiemstra at geo.uu.nl
Tue Aug 18 17:08:44 CEST 2009


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




More information about the R-help mailing list