[R] data.frame: return all rows where at least one...

Erik Iverson eriki at ccbr.umn.edu
Fri Aug 6 15:13:05 CEST 2010


On 08/06/2010 08:03 AM, Werner W. wrote:
> Hi,
>
> I know ways to do this but they all seem awkward and I somehow believe that
> there is a convenient shortcut.
>

Since you don't show us what you tried, I don't know what you consider 'awkward'.


> If I have a data.frame with many columns, how can I request all rows for which
> at least one column satisfy an expression?
>
> For instance, all rows where at least one column is negative.

A small, reproducible example in R is far better to work from than English 
descriptions.

Does your data.frame consist entirely of numeric variables?  It sounds like it,
in which case, you can use apply, which will coerce your data.frame to a matrix.

df1 <- data.frame(...)

df1[apply(df1, 1, function(x) any(x) < 0), ]



More information about the R-help mailing list