Skip to content

How to identify the rows in my dataframe with a negative value in any column?

5 messages · Mark, Henrique Dallazuanna, Steve Lianoglou +1 more

#
Try this:

DF[rowSums(DF < 0) == 0,]
On Tue, Nov 24, 2009 at 5:58 PM, Mark Na <mtb954 at gmail.com> wrote:

  
    
#
Hi,
On Nov 24, 2009, at 2:58 PM, Mark Na wrote:

            
Imagine you had a data.frame like this:

R> df <- data.frame(a=1:10, b=c(1:3,-4, 5:10), c=c(-1, 2:10))

This will return you a boolean vector of which rows have negative values:

R> has.neg <- apply(df, 1, function(row) any(row < 0))

If you want the actually index numbers:
R> which(has.neg)
[1] 1 4

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
  |  Memorial Sloan-Kettering Cancer Center
  |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
#
On Nov 24, 2009, at 3:04 PM, Henrique Dallazuanna wrote:

            
ITYM:
  > DF <- data.frame(a=1:10, b=c(1:3,-4, 5:10), c=c(-1, 2:10))
 >
 > DF[rowSums(DF < 0)  > 0,]
   a  b  c
1 1  1 -1
4 4 -4  4

--  
David
David Winsemius, MD
Heritage Laboratories
West Hartford, CT