[R] Incorporating cumsum in for loop
Uwe Ligges
ligges at statistik.tu-dortmund.de
Thu Feb 26 11:55:08 CET 2009
joe1985 wrote:
> Hello
>
> Hello i have this DF:
>
>> c
> CHR_NR
> diffdato1 x
> 1022 10395 1994-05-30 11.0 39 0 1994 1 1
> 0 0 24 1 1 24
> 29100 11377 2003-05-22 17.0 24 0 2003 1 1
> 0 0 15 1 1 29
> 29409 11377 2003-06-06 18.8 15 0 2003 1 0
> 0 0 14 0 1 29
> 28964 11377 2003-07-04 20.1 14 0 2003 1 1
> 0 0 17 1 1 59
> 29422 11377 2003-07-21 32.9 17 0 2003 1 0
> 0 0 14 0 1 59
> 28605 11377 2003-08-04 19.9 14 0 2003 1 0
> 0 0 14 0 1 59
> 28996 11377 2003-08-18 10.5 14 0 2003 1 0
> 0 0 14 0 1 59
> 29932 11377 2003-12-08 20.5 27 0 2003 1 1
> 0 0 29 1 1 78
> 30393 11377 2004-01-06 28.6 29 0 2004 1 0
> 0 0 8 0 1 78
> 36100 11377 2004-01-14 28.6 8 0 2004 1 0
> 0 0 8 0 1 78
> 30847 11377 2004-01-22 19.0 8 0 2004 1 0
> 0 0 7 0 1 78
> 34549 11377 2004-01-29 19.0 7 0 2004 1 0
> 0 0 7 0 1 78
> 30035 11377 2004-02-05 14.4 7 0 2004 1 0
> 0 0 7 0 1 78
> 34550 11377 2004-02-12 14.4 7 0 2004 1 0
> 0 0 12 0 1 78
> 42629 11493 2007-05-31 11.8 31 0 2007 1 1
> 0 0 25 1 1 25
> 20900 12558 2000-06-30 49.8 38 0 2000 1 1
> 0 0 27 1 1 118
> 21618 12558 2000-07-27 51.0 27 0 2000 1 0
> 0 0 14 0 1 118
> 22014 12558 2000-08-10 36.6 14 0 2000 1 0
> 0 0 14 0 1 118
> 21405 12558 2000-08-24 18.6 14 0 2000 1 0
> 0 0 14 0 1 118
> 21790 12558 2000-09-07 21.9 14 0 2000 1 0
> 0 0 14 0 1 118
> 22185 12558 2000-09-21 14.4 14 0 2000 1 0
> 0 0 35 0 1 118
> 16695 13018 1999-02-09 19.0 33 0 1999 1 1
> 0 0 14 1 1 14
> 22315 13018 2000-08-18 21.8 36 0 2000 1 1
> 0 0 28 1 1 81
> 22029 13018 2000-09-15 26.2 28 0 2000 1 0
> 0 0 14 0 1 81
> 21280 13018 2000-09-29 20.2 14 0 2000 1 0
> 0 0 14 0 1 81
> 21738 13018 2000-10-13 10.1 14 0 2000 1 0
> 0 0 25 0 1 81
> 24610 13018 2001-09-17 31.3 27 0 2001 1 1
> 0 0 17 1 1 32
> 23958 13018 2001-10-04 15.2 17 0 2001 1 0
> 0 0 15 0 1 32
> 43479 13168 2007-10-03 15.6 33 0 2007 1 1
> 0 0 35 1 1 35
> 44755 13168 2008-04-04 18.3 23 0 2008 1 1
> 0 0 25 1 1 25
> 45355 13168 2008-07-04 15.2 36 0 2008 1 1
> 0 0 32 1 1 66
> 45540 13168 2008-08-05 10.3 32 0 2008 1 0
> 0 0 34 0 1 66
>
> And I want like this for every x-value, illustrated with x=118:
>
>> a
> CHR_NR diffdato diffCHR aar ind10 diffind10 dato63 diffdato63
> diffdato1 x akkusum
> 12558 2000-06-30 49.8 38 0 2000 1 1 0
> 0 27 1 1 118 27 12558 2000-07-27 51.0 27
> 0 2000 1 0 0 0 14 0 1 118 41
> 12558 2000-08-10 36.6 14 0 2000 1 0 0
> 0 14 0 1 118 55
> 12558 2000-08-24 18.6 14 0 2000 1 0 0
> 0 14 0 1 118 69
> 12558 2000-09-07 21.9 14 0 2000 1 0 0
> 0 14 0 1 118 83
> 12558 2000-09-21 14.4 14 0 2000 1 0 0
> 0 35 0 1 118 118
>
> So I want the vector "akkusum" just for the whole dataset, i thought of
> using af for loop with cumsum (which i have used in the example with x=118
> where i did a$akkusum <- cumsum(a$diffdato1)), but i don't know how to set
> it up.
>
> Hope anyone can help me.
>
Example, given the names you are talking about are data.frame c and a
and elements x, diffdato1 and akkusum:
temp <- split(c, c$x)
temp2 <- lapply(temp,
function(i){
i$akkusum <- cumsum(i$diffdato1)
i
})
a <- unsplit(temp2, c$x)
Uwe Ligges
More information about the R-help
mailing list