[R] removing the rows with negative elements

Petr PIKAL petr.pikal at precheza.cz
Wed Jan 6 08:51:56 CET 2010


Another option is

 x[rowSums(x<0)==0, ]

but beware of  floating point if your numbers can be near zero. 

Regards
Petr


r-help-bounces at r-project.org napsal dne 06.01.2010 06:52:48:

> Thank you!
> 
> On Jan 6, 2010 12:31am, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> > x[apply(x,1,function(x)all(x>=0)),]
> 
> 
> 
> > -Peter Ehlers
> 
> 
> 
> > Simon Blomberg wrote:
> 
> 
> > x[-which(x
> 
> 
> > but I'm sure someone will suggest an easier way.
> 
> 
> 
> > Simon.
> 
> 
> 
> > On Wed, 2010-01-06 at 05:13 +0000, faridamsb at gmail.com wrote:
> 
> 
> > Hello All,
> 
> 
> 
> > I would like to remove the entire row, if there is any negative 
element 
> > in that row. What is the best way to do that?
> 
> 
> 
> > For example,
> 
> 
> 
> > x7,4,2,1,0), 4, 3)
> 
> 
> 
> > the returning matrix should look like
> 
> 
> 
> > [,1] [,2] [,3]
> 
> > [1,] 2 5 4
> 
> > [2,] 3 7 0
> 
> 
> 
> 
> 
> > Thank you in advance,
> 
> 
> 
> > FM
> 
> 
> 
> > [[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.
> 
> 
> 
> 
> 
> 
> 
> > --
> 
> > Peter Ehlers
> 
> > University of Calgary
> 
> > 403.202.3921
> 
> 
>    [[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.



More information about the R-help mailing list