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
__
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]
>
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
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
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
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
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
7 matches
Mail list logo