[R] indices of rows containing one or more elements >0

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Feb 13 05:35:51 CET 2008


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 >0

which(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



More information about the R-help mailing list