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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:55:08+00:00 2026-06-13T10:55:08+00:00

Possible Duplicate: How to speed up cummulative sum within group? In the following data

  • 0

Possible Duplicate:
How to speed up cummulative sum within group?

In the following data frame

id<-c(1,1,1,1,1,3,3,3,3)
spent<-c(10,20,30,40,50,60,70,80,90)
date<-c("11-11-07","11-11-07","23-11-07","12-12-08","17-12-08","11-11-07","23-11-07","23-       11-07","16-01-08")
df<-data.frame(id,date,spent)
df$date2<-as.Date(as.character(df$date), format = "%d-%m-%y")

  id     date spent      date2
1  1 11-11-07    10 2007-11-11
2  1 11-11-07    20 2007-11-11
3  1 23-11-07    30 2007-11-23
4  1 12-12-08    40 2008-12-12
5  1 17-12-08    50 2008-12-17
6  3 11-11-07    60 2007-11-11
7  3 23-11-07    70 2007-11-23
8  3 23-11-07    80 2007-11-23
9  3 16-01-08    90 2008-01-16

I need to find the maximum of spent for each id in each day and record in in a separate column as follow:

  id     date spent      date2   sum.spent
1  1 11-11-07    10 2007-11-11    20
2  1 11-11-07    20 2007-11-11    20 
3  1 23-11-07    30 2007-11-23    30
4  1 12-12-08    40 2008-12-12    40
5  1 17-12-08    50 2008-12-17    50
6  3 11-11-07    60 2007-11-11    60
7  3 23-11-07    70 2007-11-23    80
8  3 23-11-07    80 2007-11-23    80
9  3 16-01-08    90 2008-01-16    90 

Can anyone help me with 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-13T10:55:09+00:00Added an answer on June 13, 2026 at 10:55 am

    Here is your plyr answer:

    library(plyr)
    ddply(df, .(id, date), transform, sum.spent = max(spent))
    

    This is the data.table answer (better for larger datasets):

    library(data.table)
    df <- data.table(df)
    df[, sum.spent:=max(spent), by = list(id, date)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Calculating Connection/Download Speed I put data on 3 servers for app usage.
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: How to calculate speed of our car using iphone How can I
Possible Duplicate: class System.Random .. why not static? Following on from Generated random numbers
Possible Duplicate: Read large amount of data from file in Java I have a
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Speed difference in using inline strings vs concatenation in php5? Note -
Possible Duplicate: how to calculate the current speed and average speed of user travelling
Possible Duplicate: How can I edit my code to echo the data of child's
Possible Duplicate: If vs. Switch Speed I will use c++ as an example, but

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.