[R] Remove columns
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Aug 18 16:32:55 CEST 2009
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 0
Try 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
More information about the R-help
mailing list