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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:03:34+00:00 2026-05-27T20:03:34+00:00

I would like to be able to add the difference and percent change to

  • 0

I would like to be able to add the difference and percent change to every column in a dataframe.

I’m able to get as far as melting the data and performing the calculations, but I can’t figure out how to cast or reshape it back together. I also have a sneaking suspicion that this is easily accomplished with plyr, but the n-1 rows returned by diff() gives me problems.

Using an included dataset:

library(plyr)
library(quantmod)
head(longley)

     GNP.deflator     GNP Unemployed Armed.Forces Population Year Employed
1947         83.0 234.289      235.6        159.0    107.608 1947   60.323
1948         88.5 259.426      232.5        145.6    108.632 1948   61.122
1949         88.2 258.054      368.2        161.6    109.773 1949   60.171
1950         89.5 284.599      335.1        165.0    110.929 1950   61.187
1951         96.2 328.975      209.9        309.9    112.075 1951   63.221
1952         98.1 346.999      193.2        359.4    113.270 1952   63.639

longley.m <- melt(longley, id="Year")
longley.m <- ddply(longley.m, .(variable), transform, valdiff=diff(c(NA, value)), valdelt=Delt(value))

head(longley.m)

  Year     variable value valdiff Delt.1.arithmetic
1 1947 GNP.deflator  83.0      NA                NA
2 1948 GNP.deflator  88.5     5.5       0.066265060
3 1949 GNP.deflator  88.2    -0.3      -0.003389831
4 1950 GNP.deflator  89.5     1.3       0.014739229
5 1951 GNP.deflator  96.2     6.7       0.074860335
6 1952 GNP.deflator  98.1     1.9       0.019750520

(I don’t know why Delt makes it’s own column name, but I’ve given up on that)

Now, I can cast(longley.m, Year ~ variable) to get back to the original dataset, but I want to be able to have the difference and percent change for each variable in a different column without performing the calculation manually on each variable and then rbinding it back together. I’m pretty confident I’ve tried every variation of cast to no avail…

Update:
Joran solved the Delt column naming issue: coerce it with as.vector!

  • 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-27T20:03:34+00:00Added an answer on May 27, 2026 at 8:03 pm

    I’d likely approach this more like @joran.

    But if you want to continue along the path you were on, you could use reshape() from base R to complete the journey:

    # Your code
    library(plyr)
    library(quantmod)
    library(reshape)
    head(longley)
    longley.m <- melt(longley, id="Year")
    
    # My addition
    longley.m <- ddply(longley.m, .(variable), transform, 
                       valdiff = diff(c(NA, value)), 
                       valdelt = as.vector(Delt(value)))
    reshape(longley.m, idvar="Year", timevar="variable", direction="wide")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to add a message to a unit test,
I would like to be able to add a new SQL LOGIN and name
I would like to be able to add a click event to a entity
I would like to be able to add to a custom class in the
I would like to add a uitoolbar able to slide -in from the top
I would like to be able to add a score, in a different view
I would like to be able to add a non-selectable element to a list
Would like to be able to set colors of headings and such, different font
I would like to be able to loop through all of the defined parameters
We would like to be able to nightly make a copy/backup/snapshot of a production

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.