Re: [R] remove columns containing all zeros (or other value)

2009-01-15 Thread Jim Lemon
How about: remove.constant.values<-function(x,MARGIN,value2remove) { is.constant.line<-function(x,value2remove) { return(any(x!=value2remove)) } return(unlist(apply(x,MARGIN,is.constant.line,value2remove))) } x[,remove.constant.values(x,2,0)] Jim __

Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread andrew
or this x[,!(colSums(abs(x)) == 0)] On Jan 15, 10:00 am, Marc Schwartz wrote: > Careful: > > x <- matrix(c(1, 5, 3, 2, 1, 4, -1, 0, 1), >             ncol = 3, nrow = 3) > > > x > >      [,1] [,2] [,3] > [1,]    1    2   -1 > [2,]    5    1    0 > [3,]    3    4    1 > > > x[, colSums(x) != 0] >

Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread Marc Schwartz
Careful: x <- matrix(c(1, 5, 3, 2, 1, 4, -1, 0, 1), ncol = 3, nrow = 3) > x [,1] [,2] [,3] [1,]12 -1 [2,]510 [3,]341 > x[, colSums(x) != 0] [,1] [,2] [1,]12 [2,]51 [3,]34 Not quite the result wanted... :-) Try th

Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread Gustavo Carvalho
Sorry for the double post, but this is probably faster: x[, colSums(x) != 0] On Wed, Jan 14, 2009 at 8:22 PM, Gustavo Carvalho wrote: > You can also try this: > > x[,-(which(colSums(x) == 0))] > > Cheers, > > Gustavo. > > On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick wrote: >> Hello- >> >> I wo

Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread Gustavo Carvalho
You can also try this: x[,-(which(colSums(x) == 0))] Cheers, Gustavo. On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick wrote: > Hello- > > I would like to remove the columns of a matrix that contain all zeros. For > example, from > x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) > > I would like t

Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread Jorge Ivan Velez
Hi Anthony, Try this: x[,apply(x,2,function(x) !all(x==0))] HTH, Jorge On Wed, Jan 14, 2009 at 5:01 PM, Anthony Dick wrote: > Hello- > > I would like to remove the columns of a matrix that contain all zeros. For > example, from > x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) > > I would l

[R] remove columns containing all zeros (or other value)

2009-01-14 Thread Anthony Dick
Hello- I would like to remove the columns of a matrix that contain all zeros. For example, from x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3) I would like to remove the third column. However, because this is in a loop I need a way to first determine which columns are all zeros, and only the