Cumulative sum by group in sqldf?
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. 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