I have a vector containing simple time series data (extracted from a deSolve matrix), which for testing purposes can be:
x <- c(1, 2, 3, 4, 5)
and would like to apply the nonlinear filter
x[n]*x[n]-x[n-1]*x[n+1]
to all elements of the vector except the first and last elements because the filter can’t be applied to these two elements (e.g., when the x[n-1] term meets the first element or the x[n+1] term meets the last element). Therein lies my problem.
Things I’ve tried:
1) The filter() command expects a linear filter (i.e., without multiplication of filter coefficients).
2) lapply() requires that the function applies to all elements of the list.
Is a loop the only alternative?
Thanks for your help,
Carey
Can do it with loop or apply or vectorized.
vectorization is the most efficient but code is harder to decipher