[R] Odp: Remove columns

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 19 09:13:54 CEST 2009


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    0


a[,!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 guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list