Dear R-community, I have matrices/tables of different sizes which may contain rows with only zeros. Now I would like to delete these zero lines or create new matrices composed only of the non-zero lines. Columns only containing zeros I want to preserve. Here an example: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 1 0 1 1 0 0 0 [2,] 0 0 0 0 0 0 0 [3,] 1 0 0 0 0 0 0 [4,] 1 0 1 1 1 1 1 [5,] 1 0 1 0 1 0 1 [6,] 0 0 1 1 1 1 1 [7,] 0 0 0 0 0 0 0 Rows 2 and 7 shall be deleted, but column 2 shall be maintained. I believe this should be a simple operation with basic commands, but have no idea how to manage it. Looking forward do any help, Patrick
See ?apply M2 <- M[ apply(M!=0, 1, any), , drop=FALSE] Gabor On Tue, Apr 22, 2008 at 11:52:08AM +0200, Patrick Zimmermann wrote:> Dear R-community, > I have matrices/tables of different sizes which may contain rows with > only zeros. Now I would like to delete these zero lines or create new > matrices composed only of the non-zero lines. Columns only containing > zeros I want to preserve. > Here an example: > > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 1 0 1 1 0 0 0 > [2,] 0 0 0 0 0 0 0 > [3,] 1 0 0 0 0 0 0 > [4,] 1 0 1 1 1 1 1 > [5,] 1 0 1 0 1 0 1 > [6,] 0 0 1 1 1 1 1 > [7,] 0 0 0 0 0 0 0 > > Rows 2 and 7 shall be deleted, but column 2 shall be maintained. > I believe this should be a simple operation with basic commands, but > have no idea how to manage it. > Looking forward do any help, > Patrick > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
try this:
mat <- sample(0:1, 49, TRUE)
dim(mat) <- c(7, 7)
mat[c(1,4), ] <- 0
ind <- rowSums(mat == 0) != ncol(mat)
mat[ind, ]
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Patrick Zimmermann" <brassnotdead at googlemail.com>
To: "r-help" <R-help at stat.math.ethz.ch>
Sent: Tuesday, April 22, 2008 11:52 AM
Subject: [R] removing zero rows from matrix/table
> Dear R-community,
> I have matrices/tables of different sizes which may contain rows
> with
> only zeros. Now I would like to delete these zero lines or create
> new
> matrices composed only of the non-zero lines. Columns only
> containing
> zeros I want to preserve.
> Here an example:
>
> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,] 1 0 1 1 0 0 0
> [2,] 0 0 0 0 0 0 0
> [3,] 1 0 0 0 0 0 0
> [4,] 1 0 1 1 1 1 1
> [5,] 1 0 1 0 1 0 1
> [6,] 0 0 1 1 1 1 1
> [7,] 0 0 0 0 0 0 0
>
> Rows 2 and 7 shall be deleted, but column 2 shall be maintained.
> I believe this should be a simple operation with basic commands, but
> have no idea how to manage it.
> Looking forward do any help,
> Patrick
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm