[R] deleting collumns with colSums==0

jim holtman jholtman at gmail.com
Thu Apr 26 14:27:00 CEST 2007


I assume that you mean you want the columns deleted.  If so, this might work:

> set.seed(1)
> x <- matrix(sample(0:1,100,replace=TRUE,prob=c(.9,.1)), ncol=10)
> x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    1    0    0    0    1    0    0     0
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    0    0    0    0    0    0    0    0     0
 [4,]    1    0    0    0    0    0    0    0    0     0
 [5,]    0    0    0    0    0    0    0    0    0     0
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    1    0    0    0    0    0    0    0    0     0
 [8,]    0    1    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    1    0     0
> colSums(x)
 [1] 2 1 1 0 0 0 1 1 0 0
> # delete columns with zero sums
> x[, which(colSums(x) != 0)]
      [,1] [,2] [,3] [,4] [,5]
 [1,]    0    0    1    1    0
 [2,]    0    0    0    0    0
 [3,]    0    0    0    0    0
 [4,]    1    0    0    0    0
 [5,]    0    0    0    0    0
 [6,]    0    0    0    0    0
 [7,]    1    0    0    0    0
 [8,]    0    1    0    0    0
 [9,]    0    0    0    0    0
[10,]    0    0    0    0    1
>


On 4/26/07, Milton Cezar Ribeiro <milton_ruser at yahoo.com.br> wrote:
> Dear all,
>
> I have some matrices which colSums are equal to zero and I would like to delete all them.
> How can I do that?
>
> Kind regards,
>
> miltinho
> Brazil
>
> __________________________________________________
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list