Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cumulative sum over time

I have this data:

thedat <- structure(list(id = c("         w12", "         w12", "         w12", 
"          w11", "           w3", "          w3", "        w12", 
"         w45", "        w24", "       w24", "        w24", "        w08", 
"         w3", "         w3", "         w11"), time = structure(c(1559329080, 
1559363580, 1559416140, 1559329380, 1559278020, 1559413920, 1559285100, 
1559322660, 1559417460, 1559450220, 1559500980, 1559500980, 1559278020, 
1559413920, 1559276700), class = c("POSIXct", "POSIXt"), tzone = ""), 
    x = c(28.03333, 18.45, 3.85, 27.95, 42.216667, 4.466667, 
    64.25, 53.81667, 27.483333, 18.383333, 4.283333, 4.28333336, 
    66.21667, 28.46667, 66.58333)), .Names = c("id", "time", 
"x"), class = "data.frame", row.names = c(NA, -15L))

For each id I wan to get the cumulative sum for x over time. so for id = w11 I would get:

w11, 2019-05-31 05:25:00,  66.58333,
w11, 2019-05-31 20:03:00,  94.48333

I tried

ddply(thedat, .(id), summarise, 
               time = unique(time), 
               answer = cumsum(x))

but this is not getting me what I want. any help appreciated.

like image 888
user1322296 Avatar asked Oct 02 '22 14:10

user1322296


1 Answers

The problem are the different numbers of spaces in your id strings. Remove them:

thedat$id <- gsub(" ", "", thedat$id)
thedat <- thedat[order(thedat$time),]

It seems more sensible to use transform instead of summarise if you calculate the cummulative sum:

library(plyr)
ddply(thedat, .(id), transform, 
      answer = cumsum(x))

    id                time         x     answer
1  w08 2019-06-02 20:43:00  4.283333   4.283333
2  w11 2019-05-31 06:25:00 66.583330  66.583330
3  w11 2019-05-31 21:03:00 27.950000  94.533330
4  w12 2019-05-31 08:45:00 64.250000  64.250000
5  w12 2019-05-31 20:58:00 28.033330  92.283330
6  w12 2019-06-01 06:33:00 18.450000 110.733330
7  w12 2019-06-01 21:09:00  3.850000 114.583330
8  w24 2019-06-01 21:31:00 27.483333  27.483333
9  w24 2019-06-02 06:37:00 18.383333  45.866666
10 w24 2019-06-02 20:43:00  4.283333  50.149999
11  w3 2019-05-31 06:47:00 42.216667  42.216667
12  w3 2019-05-31 06:47:00 66.216670 108.433337
13  w3 2019-06-01 20:32:00  4.466667 112.900004
14  w3 2019-06-01 20:32:00 28.466670 141.366674
15 w45 2019-05-31 19:11:00 53.816670  53.816670
like image 159
Roland Avatar answered Oct 05 '22 12:10

Roland