[R] How to subset my dataframe? (a bit tricky)

Mark Na mtb954 at gmail.com
Tue Jun 16 20:26:50 CEST 2009


Hi R-helpers,

I would like to subset my dataframe, keeping only those rows which
satisfy the following conditions:

1) the string "dnv" is found in at least one column;
2) the value in the column previous to the one "dnv" is found in is not "0"

Here's what my data look like:

    POND_ID 2009-05-07 2009-05-15 2009-05-21 2009-05-28 2009-06-04

4       101       0.15          0        dnv        dnv        dnv
7       102          0        dnv        dnv        dnv        dnv
87      103       0.15        dnv          1          1          1
99      104        dnv       0.25          1          1       0.75

So, for above example, the new dataframe would not contain POND_ID 101
or 102 (because there is a 0 before the dnv) but it WOULD contain
POND_ID 103 (because there is a 0.15 before the dnv) and 104 (because
dnv occurs in the first column, so cannot be preceded by a 0).

One extra twist: I would like to retain rows in the new dataframe
which satisfy the above conditions even if they also have a "0" then
"dnv" sequence preceding or following the "problem" , e.g., the
following rows would be retained in the new dataframe

   POND_ID 2009-05-07 2009-05-15 2009-05-21 2009-05-28 2009-06-04

100     105       0.15        dnv          1          0        dnv
101     106       0           dnv          1          0.15     dnv

Thanks in advance for any help you might provide.

(I hope I've provided enough of an example; I could also provide a
.csv file if that would help.)

Mark Na




More information about the R-help mailing list