Re: [S] Re: Extracting rows of a matrix with positive elements

Prof Brian Ripley (ripley@stats.ox.ac.uk)
Sat, 21 Feb 1998 16:06:50 +0000 (GMT)


Rolf Turner wrote:

[ From a message by David Krantz ]
> > A method that will work, on a matrix denoted y, is
> >
> > y[(((y > 0) - 1) %*% rep(1, ncol(y))) == 0, ].
> >
> > Perhaps someone else will come up with an easier way.
>
> I think that
>
> y[apply(y>0,1,all),]
>
> is sexier, if not easier.

My (privately emailed) solution was

A[apply(A, 1, function(x) all(x >= 0)), ]

which has the advantage (by design) of not needing to create a matrix-sized
intermediate result like y > 0. (The original question was very vague;
is 0 positive? it could have meant any or all, ...)

As to how to find out: read some good books on S programming and study the
examples. Two of the four S programming books on my shelf had examples of
all() and apply() ....

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news