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 7533075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:38:13+00:00 2026-05-30T05:38:13+00:00

Trying to learn R and am stuck on an autocorrelation example. I want to

  • 0

Trying to learn R and am stuck on an autocorrelation example. I want to regress the difference in x against the difference in y. I have x and y in a data frame, and would like the difference of x2 – x1 to be saved in a new column say dx. I have no idea how to go about this.

what I have:

data1

x   y
5   3
8   9
3   1
1   5
.   .
.   .
.   .

what I would like to get:

data1.dif

x   y   dx   dy
5   3   NA   NA
8   9    3    6
3   1   -5   -8
1   5   -2    4
.   .    .    .
.   .    .    .
  • 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-30T05:38:14+00:00Added an answer on May 30, 2026 at 5:38 am

    Use diff, and stick an NA to the beginning of the resulting vectors.

    e.g.

    data1 <- read.table(text='  x y
    1 5 3
    2 8 9
    3 3 1
    4 1 5')
    
    # diff calculates the difference between consecutive pairs of 
    #  vector elements
    diff(data1$x)
    [1]  3 -5 -2
    
    # apply diff to each column of data1, bind an NA row to the beginning,
    #  and bind the resulting columns to the original df
    data1.dif <- cbind(data1, rbind(NA, apply(data1, 2, diff)))
    names(data1.dif) <- c('x', 'y', 'dx', 'dy')
    
    data1.dif
      x y dx dy
    1 5 3 NA NA
    2 8 9  3  6
    3 3 1 -5 -8
    4 1 5 -2  4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn Lisp but I got stuck by this example (you can
I'm trying to learn FubuMVC and have gotten stuck on validating my input models.
I'm trying to learn some javascript and have gotten stuck. Right now, I have
I am trying to learn backbone.js through the following example. Then I got stuck
I'm trying to learn C at got stuck with datatype-sizes at the moment. Have
So i have been stuck trying to learn this Scheme language that i heard
I'm trying to learn from google code and I got stuck in some problems.
I've been trying to learn how to code in xslt and currently am stuck
I've been trying to learn C, and I'm stuck on including libraries. I need

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.