Delete columns where all values are 0

r

Solution

How about this, using `apply` and `all`:

M <- as.matrix(data.frame(a=runif(10),b=rep(0,10),c=runif(10),d=rep(0,10)))

M[,which(!apply(M,2,FUN = function(x){all(x == 0)}))]

Problem

I have a numeric matrix with `15000` columns. I want to completely remove the columns where all values are `0`. ``` col1 col2 col3 col4 row1 1 0 0 1 row2 3.4 0 0 2.4 row3 0.56 0 0 0 row4 0 0 0 0 ``` Here I want to delete columns `col2` and `col3`, and keep the rest. How can I do it with R? Thanks

Original source