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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:18:00+00:00 2026-05-15T12:18:00+00:00

I know this answer must be out there, but I can’t figure out how

  • 0

I know this answer must be out there, but I can’t figure out how to word the question.

I’d like to calculate the differences between values in my data.frame.

from this:

f <- data.frame(year=c(2004, 2005, 2006, 2007), value=c(8565, 8745, 8985, 8412))

  year value
1 2004  8565
2 2005  8745
3 2006  8985
4 2007  8412

to this:

  year value diff
1 2004  8565   NA
2 2005  8745  180
3 2006  8985  240
4 2007  8412 -573

(ie value of current year minus value of previous year)

But I don’t know how to have a result in one row that is created from another row. Any help?

Thanks,
Tom

  • 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-15T12:18:01+00:00Added an answer on May 15, 2026 at 12:18 pm

    There are many different ways to do this, but here’s one:

    f[, "diff"] <- c(NA, diff(f$value))
    

    More generally, if you want to refer to relative rows, you can use lag() or do it directly with indexes:

    f[-1,"diff"] <- f[-1, "value"] - f[-nrow(f), "value"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there must be a really simple answer to this question, but I
I know this is a total newbie question, but the answer may not be
I know this exact question was asked here , but the answer didn't work
I know this is somewhat subjective, but I can't find an honest answer anywhere.
I know this question has been posted before... but I haven't found any answer
Sorry that I can't find the answer through google - But I know this
Just a quick question that no doubt someone out there will know the answer
I know this must be a fairly simple question, but I haven't managed to
I have a question that must be answer several times before, but I can't
I know this isn't strictly a programming question but y'all must have experienced this.

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.