[R] Filtering data

Joerg Maeder joerg.maeder at ethz.ch
Wed Nov 7 10:58:02 CET 2001


here a way to do it

#your data (days have to be sorted!)
da <- cbind(c(1,4,5,6,9),c(10,13,20,15,13))
#the length of it
l <- dim(da)[1]
#make day-groups
gr <- cumsum(c(T,da[2:l]-da[2:l-1]>1))
#find the index of the maximum of each group
mi <- tapply(da[,2],gr,function(a)(1:length(a))[a==max(a)])
#add them to the start index of each group
mi <- c(0,cumsum(tapply(da[,2],gr,length)))[1:length(mi)]+mi
#output
da[mi,]

Matt Pocernich wrote:
> 
> Hello,
> 
> I am having difficulty filtering data.  I am working with flow data
> collected at a stream gage.  For each record, I have a date and flow
> value.  I have filtered this data to only include days when flow values
> exceed a given threshold.
> 
> Here is my problem.  Within this subset of data, I often have several
> consecutive days above the threshold.  From this group of days, I wish to
> select the record (both date and flow) containing the maximum flow.  If an
> exceedance is isolated ( the preceeding and succeeding day is below the
> threshold) I also wish to select that record.
> 
> For example from the data set
> 
> Day     Flow
> 
> 1       10
> 4       13
> 5       20
> 6       15
> 9       13
> 
> I would like the 1st, 3rd and 5th record filered.
> 
> Any ideas on how I would write such and algorithm would be appreciated.
> 
> Thanks,
> 
> Matt Pocernich
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-- 
    Joerg Maeder             IACETH              INSTITUTE
   PhD Student                              FOR ATMOSPHERIC 
  Phone: +41 1 633 36 25                 AND CLIMATE SCIENCE
 Fax: +41 1 633 10 58                  ETH ZÜRICH Switzerland
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list