[R] Removing rows of zeros from a matrix

Petr Savicky savicky at praha1.ff.cuni.cz
Thu Jun 2 17:35:28 CEST 2011


On Thu, Jun 02, 2011 at 11:23:28AM -0400, Jim Silverton wrote:
> Hi,
> Can someone tell me how to remove rows of zeros from a matrix?
> For example if I have the following matrix,
> 
> 0 0
> 0 1
> 2 8
> 0 0
> 4 56
> 
> I should end up with
> 0 1
> 2 8
> 4 56

Hi.

Try the following

  a <- matrix(c(0, 0, 2, 0, 4, 0, 1, 8, 0, 56), ncol=2)
  a[rowSums(a != 0) != 0, ]

Hope this helps.

Petr Savicky.



More information about the R-help mailing list