[R] subset dataframe based on rows

Stephen C. Upton upton at mitre.org
Fri Jan 24 02:52:15 CET 2003


Have you looked at subset?

For example,
> data(airquality)
> subset(airquality,Temp > 80 & Wind > 10 & Solar.R < 100)
    Ozone Solar.R Wind Temp Month Day
88     52      82 12.0   86     7  27
94      9      24 13.8   81     8   2
129    32      92 15.5   84     9   6

HTH
steve

"J.R. Lockwood" wrote:

> >
> > 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:
>
> d[meetsyourconditions,]
>
> 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
> dataframe.
>
> J.R. Lockwood
> 412-683-2300 x4941
> lockwood at rand.org
> http://www.rand.org/methodology/stat/members/lockwood/
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list