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

William Dunlap wdunlap at tibco.com
Tue Jun 16 21:47:40 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Na
> Sent: Tuesday, June 16, 2009 11:27 AM
> To: r-help at r-project.org
> Subject: [R] How to subset my dataframe? (a bit tricky)
> 
> 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"

Suppose your data.frame is called 'd'.  Then try looping over
its columns:
    keep <- rep(FALSE, nrow(d))
    if (ncol(d)>2) for(i in 3:ncol(d)) keep <- keep | ( d[,i]=="drv" & d[,i-1]!="0")
so
    d[keep,]
is the subset you want.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 
  
> 
> 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
> 
> ______________________________________________
> 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.
> 




More information about the R-help mailing list