[R] subset dataframe based on rows

J.R. Lockwood lockwood at rand.org
Fri Jan 24 00:11:02 CET 2003

> I want to subset the dataframe based on certain values in a row.
> for each row in my dataframe
> 	if ANY one value of a particular set of columns satisfies cond
> 		append a logical value true at the end of the row
> 	else
> 		append a false at the end of the row
> in the end I want to be able to subset the whole data based on the
> appended true or false value.
> I could literally code like this, but I think there must be a better way
> to do this. Can someone give me a hint?? thanks.
> Lei

I'm not sure what you mean by "better", but at the least you don't
need to append the indicator column to your dataframe.  Just use
whatever logical vector results from checking whether the columns
satisfy the condition to subset your data frame, as in:


As for determining whether the conditions are met, row by row, you
should be able to this as a vector operation, but it could get ugly
depending on the nature of the columns and what you call "cond".  In
the most fortunate case where all the columns are of a single mode and
cond is sufficiently simple, you can apply "function(x){any(x meets
cond)}" to the rows of a matrix defined by your columns of interest.
This will give you the logical vector you need to subset the

J.R. Lockwood
412-683-2300 x4941
lockwood at rand.org

More information about the R-help mailing list