Hi. I have an n x m matrix M some of who's entries are zeros. I want to know how many zeros there are in each row -perhaps stored in a 1 x n vector which lists the number of zeros for each row of M. Before I had a vector V and I was able to get the number of zeros in V by doing length(V[ V==0]) but when I try something similar for M, like M[ M==0] it creates a vector not a matrix and so this does not work. Does anyone have a solution to this? Thank you. -- View this message in context: http://www.nabble.com/number-of-zeros-in-a-matrix--row-by-row-tp22893147p22893147.html Sent from the R help mailing list archive at Nabble.com.
if 'M' is your matrix, then try this: rowSums(M == 0) I hope it helps. Best, Dimitris onyourmark wrote:> Hi. I have an n x m matrix M some of who's entries are zeros. I want to know > how many zeros there are in each row -perhaps stored in a 1 x n vector > which lists the number of zeros for each row of M. > > Before I had a vector V and I was able to get the number of zeros in V by > doing length(V[ V==0]) but when I try something similar for M, like M[ M==0] > it creates a vector not a matrix and so this does not work. Does anyone have > a solution to this? > > Thank you.-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Bill.Venables at csiro.au
2009-Apr-05 23:02 UTC
[R] number of zeros in a matrix -row by row
> M <- matrix(0:15, 8, 4, byrow=TRUE) > rowSums(M == 0)[1] 1 0 0 0 1 0 0 0 Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of onyourmark Sent: Sunday, 5 April 2009 10:00 PM To: r-help at r-project.org Subject: [R] number of zeros in a matrix -row by row Hi. I have an n x m matrix M some of who's entries are zeros. I want to know how many zeros there are in each row -perhaps stored in a 1 x n vector which lists the number of zeros for each row of M. Before I had a vector V and I was able to get the number of zeros in V by doing length(V[ V==0]) but when I try something similar for M, like M[ M==0] it creates a vector not a matrix and so this does not work. Does anyone have a solution to this? Thank you. -- View this message in context: http://www.nabble.com/number-of-zeros-in-a-matrix--row-by-row-tp22893147p22893147.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Hi, Try /rowSums(M==0)/ This should work just fine. Cheers, Luc onyourmark wrote:> Hi. I have an n x m matrix M some of who's entries are zeros. I want to know > how many zeros there are in each row -perhaps stored in a 1 x n vector > which lists the number of zeros for each row of M. > > Before I had a vector V and I was able to get the number of zeros in V by > doing length(V[ V==0]) but when I try something similar for M, like M[ M==0] > it creates a vector not a matrix and so this does not work. Does anyone have > a solution to this? > > Thank you. >