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

  • Home
  • SEARCH
  • 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 7774397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:22:05+00:00 2026-06-01T17:22:05+00:00

diff() calculates the difference between values in a vector at a specified lag. Is

  • 0

diff() calculates the difference between values in a vector at a specified lag.
Is there an equivalent function that works on two vectors? For example, I have:

v1 = c(1, 2, 3, 4, 5, 3)
v2 = c(5, 4, 3, 2, 1, 0)

I need to calculate the difference between each value of v1 and v2 at lag 1. That would be:

(2 - 5), (3 - 4), (4 - 3)... 

This can be achieved using combinations of head()/tails() on the 2 vectors, but I was wondering if there is already a function that can do the same.

  • 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-06-01T17:22:07+00:00Added an answer on June 1, 2026 at 5:22 pm

    There’s no base function I know of to do this but as gsk3 pointed out the taRifx package has this capability. I would advise against calling a package to do something this simple: You could do:

    v1[-1] - v2[-length(v2)]
    

    Or write your own function for storage in .Rprofile

    shift.diff <- function(x, y) x[-1] - y[-length(y)]
    shift.diff(v1, v2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a bit of code here that calculates the hours difference between two
I'm currently working on a class that calculates the difference between two objects. I'm
i want to know the time difference between two countries. There is ofcourse the
I am writing a PHP function that calculates the difference the current time and
Can anyone tell me how to calculate the diff between two dates without using
git diff master..lab It will produce the diff between the tips of the two
Within a java program, I want to calculate the diff between two versions of
I am trying to calculate the difference between two dates. This is what I'm
I am trying to calculate difference between two date strings both of which are
I am trying to calculate the difference between given two times and i find

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.