Try this: > x <- matrix(1:40,ncol=5) > # function to find zero rows > f.zero <- + function(arr) + { + which(apply(arr, 1, function(z) all(z == 0)))[1] # first one + } > > # now the non-zero rows > f.nonzero <- + function(arr) + { + which(apply(arr, 1, function(z) any(z != 0))) + } > > f.zero(x) [1] NA > f.nonzero(x) [1] 1 2 3 4 5 6 7 8 > # now make one zero > x[3,] <- 0 > f.zero(x) [1] 3 > f.nonzero(x) [1] 1 2 4 5 6 7 8 > # more than one > x[1,] <- 0 > f.zero(x) [1] 1 > f.nonzero(x) [1] 2 4 5 6 7 8 >
On Mon, Aug 10, 2009 at 6:15 AM, kaixin maleA<kaixinma...@gmail.com> wrote: > Dear all, > > I have a task to find the first all zero row of a matrix X ( nothing known > about X). I need to write a function which returns either the row index of > the first all-zero row, or NA if there are no all-zero rows. and I also need > to locate all rows which are non-zero (should be a vector of row indexes). > > Can somebody give me some hints on this? > > Thanks a lot. > > Rene. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ R-help@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.