Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:48:50+00:00 2026-05-20T07:48:50+00:00

I have a vector containing simple time series data (extracted from a deSolve matrix),

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:48:50+00:00Added an answer on May 20, 2026 at 7:48 am

    Can do it with loop or apply or vectorized.

    > x <- c(1, 2, 3, 4, 5)
    > r <- NA
    > for (n in 2:length(x)) r[n] <- x[n]*x[n]-x[n-1]*x[n+1]
    > (r)
    [1] NA  1  1  1 NA
    > 
    > r <- NA
    > lapply(2:length(x),function(n) r[n] <<- x[n]*x[n]-x[n-1]*x[n+1])
    [[1]]
    [1] 1
    
    [[2]]
    [1] 1
    
    [[3]]
    [1] 1
    
    [[4]]
    [1] NA
    
    > (r)
    [1] NA  1  1  1 NA
    
    > r <- NA
    > r <- x^2 - c(NA,x[1:(length(x)-1)]) * c(x[2:length(x)],NA)
    > (r)
    [1] NA  1  1  1 NA
    

    vectorization is the most efficient but code is harder to decipher

    > x <- runif(50000)
    > 
    > r <- NA
    > system.time(for (n in 2:length(x)) r[n] <- x[n]*x[n]-x[n-1]*x[n+1])
       user  system elapsed 
       8.55    0.01    8.58 
    > 
    > r <- NA
    > system.time(lapply(2:length(x),function(n) r[n] <<- x[n]*x[n]-x[n-1]*x[n+1]))
       user  system elapsed 
      11.36    0.00   11.39 
    > 
    > r <- NA
    > system.time(r <- x^2 - c(NA,x[1:(length(x)-1)]) * c(x[2:length(x)],NA))
       user  system elapsed 
       0.01    0.00    0.01 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to apply regression function progressively to a time series data (vector time
I have a std::vector containing a handful of numbers, which are not in any
I have a map class, which contains a vector containing MapEntitys. MapEntity is a
I have a Vector containing x . What is the most elegant way to
I have designed a quick and basic vector arithmetic library in C++. I call
i am new to boost::shared_ptr using it the first time now. I have a
I have a sound file, which I'll call sndfile.wav. So far I have determined
I'm attempting to find duplicate instances of strings where I have a vector of
This is the first time I am posting a question on stackoverflow, so please
I have an ArrayAdapter (myAdapter) attached to an AutoCompleteTextView (textView) component. Once the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.