[R] How to identify the rows in my dataframe with a negative value in any column?
Steve Lianoglou
mailinglist.honeypot at gmail.com
Tue Nov 24 21:07:21 CET 2009
Hi,
On Nov 24, 2009, at 2:58 PM, Mark Na wrote:
> Dear R-helpers,
>
> I have a dataframe that should not contain any negative values, but it does.
> I wish to print the rows from my dataframe that contain a negative value in
> any column. I've tried this:
>
>> dataframe[dataframe<0,]
>
> but it just returns a row of NAs.
>
> I would very much appreciate any help with this you could provide.
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
More information about the R-help
mailing list