[R] Moving Average on Defined Intervals

Alain Zuur highstat at highstat.com
Thu Jul 30 12:53:19 CEST 2009



Hadassa Brunschwig-2 wrote:
> 
> Hi all
> 
> I have been looking (in the help archives) for a function
> which does a moving average. Nothing new, I know.
> But I am looking for a function which is very flexible:
> The user should be able to input a vector of breaks
> which define the bins (and not just the number of observations in a bin).
> The function would then calculate which observations fall into this bin
> and calculate, say, an average.
> Another parameter should be the overlap (probably only when
> the breaks of the bins are equidistant).
> 
> I am sure I am not the first one to think about this problem.
> Thanks for any hints.
> Hadassa
> 
> 
> 
> -- 
> Hadassa Brunschwig
> PhD Student
> Department of Statistics
> The Hebrew University of Jerusalem
> http://www.stat.huji.ac.il
> 
> ______________________________________________
> 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.
> 
> 


I think you have to program that yourself...can't be difficult....will only
take 30 minutes. Something like:


MyFunction <- function(Y,MyBin){

#Create a new vector of the same length as Y that defines the groupings
#Something like:  1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 6 6 6 6
6
# This is a bit of rep magic, and conditional statements.

#Apply sapply on Y using this new vector

}


Instead of this, you can also make a loop, bit it is a bit more old
fashioned.



Easy-peasy


Alain

-----
--------------------------------------------------------------------
Dr. Alain F. Zuur
First author of:

1. Analysing Ecological Data (2007).
Zuur, AF, Ieno, EN and Smith, GM. Springer. 680 p.

2. Mixed effects models and extensions in ecology with R. (2009).
Zuur, AF, Ieno, EN, Walker, N, Saveliev, AA, and Smith, GM. Springer.

3. A Beginner's Guide to R (2009).
Zuur, AF, Ieno, EN, Meesters, EHWG. Springer


Statistical consultancy, courses, data analysis and software
Highland Statistics Ltd.
6 Laverock road
UK - AB41 6FN Newburgh
Email: highstat at highstat.com
URL: www.highstat.com



-- 
View this message in context: http://www.nabble.com/Moving-Average-on-Defined-Intervals-tp24736116p24736192.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list