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 a std::vector containing a handful of numbers, which are not in any
If I have a std::vector of objects containing a rotated / translates matrix, is
I have a vector containing a series of integers, and what I want to
Suppose we have a vector/array in C++ and we wish to count which of
I have a number of vector containers of varying size each containing doubles. I
I have a number of vectors of strings each containing dates. As a simple
I have the following toy code, intended to remove duplicates from a vector: void
I have a file where a data structure containing 6 columns is stored side
I have a vector that I want to insert into a set . This
I have a vector-like class that contains an array of objects of type T

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.