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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:52:07+00:00 2026-06-09T15:52:07+00:00

Edit: This problem was due to an outdated version of data.table being installed. I

  • 0

Edit: This problem was due to an outdated version of data.table being installed.

I have a data.table like the following:

require(xts)
a <- data.table(colour=c("Red","Green","Blue","Blue","Black","Black"), date=c(as.Date("2011-07-04"),as.Date("2011-07-10"),as.Date("2011-07-09"),as.Date("2011-07-12"),as.Date("2011-07-04"),as.Date("2011-07-09")),daily.quantity=c(1,-1,2,-2,1,1))

     colour       date daily.quantity
[1,]    Red 2011-07-04              1
[2,]  Green 2011-07-10             -1
[3,]   Blue 2011-07-09              2
[4,]   Blue 2011-07-12             -2
[5,]  Black 2011-07-04              1
[6,]  Black 2011-07-09              1

I would like cumulative totals per colour, to look like this:

     colour       date daily.quantity cumulative.quantity
[1,]  Black 2011-07-04              1                   1
[2,]  Black 2011-07-09              1                   2
[3,]   Blue 2011-07-09              2                   2
[4,]   Blue 2011-07-12             -2                   0
[5,]  Green 2011-07-10             -1                  -1
[6,]    Red 2011-07-04              1                   1

However, if I try the following, I end up with cumulative totals that don’t take colour into account:

setkey(a,colour,date)
a[,cumulative.quantity := cumsum(daily.quantity)]

     colour       date daily.quantity cumulative.quantity
[1,]  Black 2011-07-04              1                   1
[2,]  Black 2011-07-09              1                   2
[3,]   Blue 2011-07-09              2                   4
[4,]   Blue 2011-07-12             -2                   2
[5,]  Green 2011-07-10             -1                   1
[6,]    Red 2011-07-04              1                   2

I tried the obvious, but unfortunately unimplemented:

> a[,cumulative.quantity := cumsum(daily.quantity),keyby="colour,date"]
Error in `[.data.table`(a, , `:=`(cumulative.quantity, cumsum(daily.quantity)),  : 
  Combining := in j with by is not yet implemented. Please let maintainer('data.table') know if you are interested in this.

So, can anyone suggest a workaround for this?

  • 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-09T15:52:08+00:00Added an answer on June 9, 2026 at 3:52 pm

    The by grouping should only be on colour:

    a[,cumulative.quantity := cumsum(daily.quantity), by=colour]
       colour       date daily.quantity cumulative.quantity
    1:  Black 2011-07-04              1                   1
    2:  Black 2011-07-09              1                   2
    3:   Blue 2011-07-09              2                   2
    4:   Blue 2011-07-12             -2                   0
    5:  Green 2011-07-10             -1                  -1
    6:    Red 2011-07-04              1                   1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

THIRD EDIT: I now believe that this problem is due to a SOAP version
Edit : The bug that caused this problem has been fixed. The @version tag
Edit: This problem was down to me passing the wrong view to the Touch
[Edit: This problem applies only to 32-bit systems. If your computer, your OS and
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
EDIT: Just to make things clear, this problem was caused by a typo in
[EDIT] I am changing this to more concisely explain what my problem was, after
This is a weird problem. I am using a dataform, and when I edit
Edit: this question is outdated. The jsonlite package flattens automatically. I am dealing with
Edit: Both the answers below work. My problem was due to using the NHibernate

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.