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 __

[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

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 ad...@uchicago.edu 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)

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 ad...@uchicago.edu 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

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 gustavo.bi...@gmail.com wrote: You can also try this: x[,-(which(colSums(x) == 0))] Cheers, Gustavo. On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick

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

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 wdwgol...@gmail.com 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) !=