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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:01:29+00:00 2026-06-03T14:01:29+00:00

Related to this question here , but I decided to ask another question for

  • 0

Related to this question here, but I decided to ask another question for the sake of clarity as the ‘new’ question is not directly related to the original. Briefly, I am using ddply to cumulatively sum a value for each of three years. My code takes data from the first year and repeats in in the second and third-year rows of the column. My guess is that each 1-year chunk is being copied to the whole of the column, but I don’t understand why.

Q. How can I get a cumulatively summed value for each year, in the right rows of the designated column?

[Edit: the for loop – or something similar – is important, as ultimately I want to automagically calculate new columns based on a list of column names, rather than calculating each new column by hand. The loop iterates over the list of column names.]

enter image description here

I use the ddply and cumsum combination frequently so it is rather vexing to suddenly be having problems with it.

[Edit: this code has been updated to the solution I settled on, which is based on @Chase’s answer below]

require(lubridate)
require(plyr)
require(xts)
require(reshape)
require(reshape2)

set.seed(12345)
# create dummy time series data
monthsback <- 24
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
                   myvalue1 = runif(monthsback, min = 600, max = 800),
                   myvalue2 = runif(monthsback, min = 1900, max = 2400),
                   myvalue3 = runif(monthsback, min = 50, max = 80),
                   myvalue4 = runif(monthsback, min = 200, max = 300))

mydf$year <- as.numeric(format(as.Date(mydf$mydate), format="%Y"))
mydf$month <- as.numeric(format(as.Date(mydf$mydate), format="%m"))

# Select columns to process
newcolnames <- c('myvalue1','myvalue4','myvalue2')

# melt n' cast
mydf.m <- mydf[,c('mydate','year',newcolnames)]
mydf.m <- melt(mydf.m, measure.vars = newcolnames)
mydf.m <- ddply(mydf.m, c("year", "variable"), transform, newcol = cumsum(value))
mydf.m <- dcast(mydate ~ variable, data = mydf.m, value.var = "newcol")
colnames(mydf.m) <- c('mydate',paste(newcolnames, "_cum", sep = ""))
mydf <- merge(mydf, mydf.m, by = 'mydate', all = FALSE)
mydf
  • 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-03T14:01:31+00:00Added an answer on June 3, 2026 at 2:01 pm

    I don’t really follow your for loop there, but are you overcomplicating things? Can’t you just directly use transform and ddply?

    #Make sure it's ordered properly
    mydf <- mydf[order(mydf$year, mydf$month),]
    
    #Use ddply to calculate the cumsum by year:
    ddply(mydf, "year", transform, 
          cumsum1 = cumsum(myvalue1), 
          cumsum2 = cumsum(myvalue2))
    #----------
           mydate myvalue1 myvalue2 year month   cumsum1   cumsum2
    1  2010-05-01 744.1808 264.4543 2010     5  744.1808  264.4543
    2  2010-06-01 775.1546 238.9828 2010     6 1519.3354  503.4371
    3  2010-07-01 752.1965 269.8544 2010     7 2271.5319  773.2915
    ....
    9  2011-01-01 745.5411 218.7712 2011     1  745.5411  218.7712
    10 2011-02-01 797.9474 268.1834 2011     2 1543.4884  486.9546
    11 2011-03-01 606.9071 237.0104 2011     3 2150.3955  723.9650
    ...
    21 2012-01-01 690.7456 225.9681 2012     1  690.7456  225.9681
    22 2012-02-01 665.3505 232.1225 2012     2 1356.0961  458.0906
    23 2012-03-01 793.0831 206.0195 2012     3 2149.1792  664.1101
    

    EDIT – this is untested as I don’t have R on this machine, but this is what I had in mind:

    require(reshape2)
    mydf.m <- melt(mydf, measure.vars = newcolnames)
    mydf.m <- ddply(mydf.m, c("year", "variable"), transform, newcol = cumsum(value))
    dcast(mydate + year + month  ~ variable, data = mydf.m, value.var = "newcol")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if i should ask this question here, but it's related to
This is related to this question here, but with a slight twist: instead of
First of all, I looked up this related question in here but the solution
This question is related to the question posted here: Why isn't my custom WCF
This is related to my previous question here . I want 4 divs (absolute
This question is related to a previous post of mine Here . Basically, I
This is related to a question I asked here: Thread Locking in Ruby (use
The title is my question. I already found a topic related to this here
This is a related to a previous question I have asked here, see the
Related to this question , I decided to check the UDFs in my data

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.