Hi, Given test <- matrix(c(0,2,0,1,3,5), 3,2)> test[test>0][1] 2 1 3 5 These are values >0> which(test>0)[1] 2 4 5 6 These are array indices of those values >0> which(apply(test>0, 1, all))[1] 2 This gives the row whose elements are all >0 I can't seem to get indices of rows containing one or more elements >0 [[alternative HTML version deleted]]
Henrik Bengtsson
2008-Feb-13 03:30 UTC
[R] indices of rows containing one or more elements >0
X <- matrix(c(0,2,0,1,0,0,3,5), ncol=2) Informative version: isPositive <- (X > 0) nbrOfPositives <- apply(isPositive, MARGIN=1, FUN=sum) hasPositives <- (nbrOfPositives >= 1) positiveRows <- which(hasPositives) Compact version: positiveRows <- which(rowSums(X > 0) >= 1) If you have an extremely large number of rows and only a few columns, you might for speed purposes want to use an algorithm that iterates over columns instead, but I leave that as an exercise. /Henrik On Feb 12, 2008 6:40 PM, Ng Stanley <stanleyngkl at gmail.com> wrote:> Hi, > > Given test <- matrix(c(0,2,0,1,3,5), 3,2) > > > test[test>0] > [1] 2 1 3 5 > > These are values >0 > > > which(test>0) > [1] 2 4 5 6 > > These are array indices of those values >0 > > > which(apply(test>0, 1, all)) > [1] 2 > > This gives the row whose elements are all >0 > > I can't seem to get indices of rows containing one or more elements >0 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Is this what you are after?> test <- matrix(c(0,2,0,1,3,5), 3,2) > (x <- which(test > 0, arr.ind=TRUE))row col [1,] 2 1 [2,] 1 2 [3,] 2 2 [4,] 3 2> unique(x[, 'row'])[1] 2 1 3>On Feb 12, 2008 9:40 PM, Ng Stanley <stanleyngkl at gmail.com> wrote:> Hi, > > Given test <- matrix(c(0,2,0,1,3,5), 3,2) > > > test[test>0] > [1] 2 1 3 5 > > These are values >0 > > > which(test>0) > [1] 2 4 5 6 > > These are array indices of those values >0 > > > which(apply(test>0, 1, all)) > [1] 2 > > This gives the row whose elements are all >0 > > I can't seem to get indices of rows containing one or more elements >0 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Berwin A Turlach
2008-Feb-13 04:35 UTC
[R] indices of rows containing one or more elements >0
G'day Stanley, On Wed, 13 Feb 2008 10:40:09 +0800 "Ng Stanley" <stanleyngkl at gmail.com> wrote:> Hi, > > Given test <- matrix(c(0,2,0,1,3,5), 3,2) > > > test[test>0] > [1] 2 1 3 5 > > These are values >0 > > > which(test>0) > [1] 2 4 5 6 > > These are array indices of those values >0 > > > which(apply(test>0, 1, all)) > [1] 2 > > This gives the row whose elements are all >0 > > I can't seem to get indices of rows containing one or more elements >0which(apply(test>0, 1 any)) instead of which(apply(test>0, 1, all)) ?? HTH. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability +65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba
Seemingly Similar Threads
- How to cbind or rbind different lengths vectors/arrays without repeating the elements of the shorter vectors/arrays ?
- how to write a table to pdf
- 3 questions: debug R script, multi-level sorts, and multi-gsub
- Conditional rows
- What objects will save.image saves ? And how to specify objects to be saved..