[R] moving average on irregular time series

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 4 13:12:51 CEST 2010


On Thu, Jun 3, 2010 at 8:04 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Replace the non-events with NA and then use na.locf from the zoo
> package to move the last event date up to give lastEvent.
> Then simply select those rows whose lastEvent date is at least 14 days
> ago or if the row itself is an Event:
>
>> library(zoo) # na.locf
>>
>> lastEvent <- with(exData, na.locf(ifelse(Event, Datebegin, NA), na.rm = FALSE))
>> exData[beg >= lastEvent + 14 | exData$Event, ]

The last line should have been:

exData[exData$Datebegin >= lastEvent + 14 | exData$Event, ]


>    Datebegin Event
> 1  2009-08-20  TRUE
> 2  2009-11-21 FALSE
> 3  2009-11-28 FALSE
> 4  2010-01-12 FALSE
> 5  2010-01-18 FALSE
> 6  2010-01-23 FALSE
> 7  2010-02-21 FALSE
> 8  2010-03-03 FALSE
> 9  2010-04-06 FALSE
> 10 2010-04-16 FALSE
> 11 2010-05-22  TRUE
> 12 2010-05-27  TRUE
> 13 2009-09-08  TRUE
> 14 2009-10-01 FALSE
> 15 2009-10-06 FALSE
> 16 2009-10-09 FALSE
> 17 2009-10-11  TRUE
> 19 2009-10-28 FALSE
> 20 2009-10-31 FALSE
>
>
> On Thu, Jun 3, 2010 at 10:23 AM, Gustaf Rydevik
> <gustaf.rydevik at gmail.com> wrote:
>> 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.
>>
>>
>>
>> --
>> 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