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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:11:45+00:00 2026-05-27T19:11:45+00:00

I have a data frame with 3 variables: place, time, and value (P, T,

  • 0

I have a data frame with 3 variables: place, time, and value (P, T, X). I want to create a fourth variable which will be the cumulative sum of X. Normally I like to do grouping calculations with sqldf, but can’t seem to find an equivalent for cumsum. That is:

sqldf("select P,T,X, cumsum(X) as X_CUM from df group by P,T") 

doesn’t work. Is this even possible with sqldf? I tried doBy, but that doesn’t all cumsum either.

  • 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-27T19:11:45+00:00Added an answer on May 27, 2026 at 7:11 pm

    Set up some test data:

    DF <- data.frame(t = 1:4, p = rep(1:3, each = 4), value = 1:12)
    

    and now we have three solutions. First we use sqldf, as requested, using the default SQLite database. Next we do it with sqldf again but this time with PostgreSQL using RPostgreSQL or RpgSQL driver. PostgreSQL supports analytical windowing functions which simplify the SQL. You will need to set up a PostgreSQL database first to do that one. Finally we show a pure R solution which only uses the core of R.

    1) sqldf/RSQLite

    library(sqldf)
    
    sqldf("select a.*, sum(b.value) as cumsum 
        from DF a join DF b 
        using (p)
        where a.t >= b.t
        group by p, a.t"
    )
    

    2) sqldf/RPostgreSQL

    library(RPostgreSQL)
    library(sqldf)
    
    sqldf('select *,
        sum(value) over (partition by p order by t) as cumsum 
        from "DF"'
    )
    

    (This also works with the RpgSQL PostgreSQL driver. To use that you must have Java installed and a PostgreSQL database set up and then in place of the above use: 1ibrary(RpgSQL); sqldf(...) where the same SQL string is used except there should be no quotes around DF.)

    3) Plain R

    transform(DF, cumsum = ave(value, p, FUN = cumsum))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with several variables. What I want is create a
I have a large data frame with date variables, which reflect first day of
I have a data frame with 3 variables, which are all wind speeds. I
I have data frame with some numerical variables and some categorical factor variables. The
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data.frame, df, with n variables in it. The variables can take
I have a data frame filled with factor columns, and I want to add
I have a data set which is similar to df <- data.frame(cbind( c(rep.int(x =
I have the following part of a data frame which is much bigger than
Lets say I have a data frame with two variables x & y and

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.