[R] help needed on data management

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 3 05:45:50 CET 2006


First create your running sum function and then tapply it to the
groups:

runsum <- function(x, k = 3) diff(c(rep(0, k), cumsum(x)), k)
DF$back <- unlist(tapply(DF$plays, DF$Userid, runsum))

Note that there is a survey of running sum/mean/etc. here:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/43050.html

On 11/2/06, Alfred Xuan <xzm_1999 at yahoo.com> wrote:
> Hi,
>
> I am quite new in R, but want to try the
> following data aggregation in R. The data has
> three columns, and the fourth is what i am trying
> to get. I created this in SAS, and would like to
> learn to do it in R.
>
> Here is some snapshot of the dataset. I have a
> list of different users, and on different dates
> (sorted), they had played several rounds of
> plays. Date here is coded ascendingly. On each
> date per user, I want to sum the total number of
> plays for a span of 3 days counting backward. For
> example, on the date of 16528 for user 1, the
> back3 should the sum of 9+3+8=20. For those dates
> that can only go back for less than 3 days, sum
> the number of plays on available dates. For
> example, on date 16527 for userid 1, there are
> only 2 days, so the sum is 3+8=11.
>
> Userid  plays   date    back3
> 1       8       16526   8
> 1       3       16527   11
> 1       9       16528   20
> 1       4       16529   16
> 1       3       16531   16
> 1       12      16532   19
> 1       3       16533   18
> 1       16      16534   31
> 1       6       16535   25
> 1       16      16536   38
> 1       10      16537   32
> 2       30      16528   30
> 2       16      16529   46
> 2       15      16530   61
> 2       11      16531   42
> 2       9       16532   35
> 2       11      16533   31
> 2       24      16534   44
> 2       21      16535   56
> 2       37      16536   82
> 2       6       16537   64
> 2       58      16538   101
> 2       34      16539   98
> 2       19      16540   111
> 2       18      16541   71
> 2       23      16542   60
> 2       35      16543   76
> 2       10      16544   68
> 2       7       16545   52
> 2       1       16546   18
> 2       15      16547   23
> 2       10      16548   26
> 2       12      16549   37
> 2       20      16550   42
> 2       18      16551   50
> 2       12      16552   50
> 2       8       16553   38
> 2       8       16554   28
> 2       32      16555   48
> 2       12      16556   52
> 2       13      16557   57
> 2       25      16610   50
> 3       1       16574   1
> 3       1       16576   2
> 3       1       16577   3
> 3       1       16578   3
> 3       2       16587   4
> 3       2       16588   5
> 3       1       16590   5
> 3       4       16602   7
> 3       1       16603   6
> 3       1       16624   6
> 3       6       16625   8
> 3       3       16626   10
> 3       4       16653   13
> 3       3       16654   10
>
> Any help is appreciated.
> Alfred
>
>
>
> __________________________________________________________________________________________
>
> (http://advision.webevents.yahoo.com/mailbeta)
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list