[R] moving average on irregular time series

William Dunlap wdunlap at tibco.com
Thu Jun 3 22:00:15 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Gustaf Rydevik
> Sent: Thursday, June 03, 2010 7:24 AM
> To: r-help at r-project.org
> Subject: [R] moving average on irregular time series
> 
> Hi all,
> 
> 
> I wonder if there is any way to calculate a moving average on an
> irregular time series, or use the rollapply function in zoo?
> I have a set of dates where I want to check if there has been an event
> 14 days prior to each time point in order to mark these timepoints for
> removal, and can't figure out a good way to do it.
> 
> Many thanks in advance!
> 
> Gustaf
> 
> 
> Example data:
> 
> exData<-structure(list(Datebegin = structure(c(14476, 14569, 
> 14576, 14621,
> 14627, 14632, 14661, 14671, 14705, 14715, 14751, 14756, 14495,
> 14518, 14523, 14526, 14528, 14529, 14545, 14548), class = "Date"),
>     Event = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
>     FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
>     TRUE, FALSE, FALSE, FALSE)), .Names = c("Datebegin", "Event"
> ), row.names = c(NA, 20L), class = "data.frame")
> 
> ###In this example, row 18 is a date less than 14 days after an event
> and should be marked for removal.

The following function returns the number of days since
the last event:

f <- function (data) {
    # if the dataset were ordered by time the order() calls
    # at the start and end would not be needed.
    o <- order(data$Datebegin)
    data <- data[o, , drop = FALSE]
    lastEventRow <- which(data$Event)[cumsum(data$Event)]
    if (length(lastEventRow) < length(o)) {
        # i.e., earliest entries are not events
        lastEventRow <- c(rep(NA, length(o) - length(lastEventRow)), 
            lastEventRow)
    }
    timeSinceLastEvent <- data$Datebegin - data$Datebegin[lastEventRow]
    timeSinceLastEvent[order(o)]
}

You can do
   tmp <- f(exData)
   exData[tmp>=14 | tmp$Event, , drop=FALSE]
to select the events and the nonevents more than
two weeks after an event.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
 
> 
> 
> -- 
> Gustaf Rydevik, M.Sci.
> tel: +46(0)703 051 451
> address:Essingetorget 40,112 66 Stockholm, SE
> skype:gustaf_rydevik
> 
> ______________________________________________
> 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