Mark Na
2009-Nov-24 19:58 UTC
[R] How to identify the rows in my dataframe with a negative value in any column?
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20091124/27851110/attachment-0001.pl>
Henrique Dallazuanna
2009-Nov-24 20:04 UTC
[R] How to identify the rows in my dataframe with a negative value in any column?
Try this: DF[rowSums(DF < 0) == 0,] On Tue, Nov 24, 2009 at 5:58 PM, Mark Na <mtb954 at gmail.com> 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. > > Thanks, Mark > > ? ? ? ?[[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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Steve Lianoglou
2009-Nov-24 20:07 UTC
[R] How to identify the rows in my dataframe with a negative value in any column?
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