[R] inverse cumsum
Alfredo Alessandrini
alfreale74 at gmail.com
Wed Jun 18 18:38:16 CEST 2008
> i guess you mean columnwise cumsums computed starting from the bottom up?
yes
> then this should do (given that your data is in matrix m, and years are
> row labels, not data):
>
> rc = nrow(m)
> cumsums = apply(m[rc:1,], 2, cumsum)[rc:1,]
ok...work very well..
>data
1987 1.33 1.21 1.77 1.44 0.27 2.85
1988 1.86 1.06 2.33 2.14 0.55 1.40
1989 2.10 0.65 2.74 2.43 1.19 1.45
1990 1.55 0.00 1.59 1.94 0.99 2.14
1991 0.92 0.72 0.50 1.29 0.54 1.22
1992 2.15 1.28 1.23 2.26 1.22 3.17
1993 1.50 0.87 1.68 1.97 0.83 2.55
1994 0.69 0.00 0.76 1.89 0.60 0.87
1995 1.13 1.04 1.19 1.52 1.13 1.78
1996 1.15 0.92 1.50 0.97 0.60 0.00
>apply(m[rc:1,], 2, cumsum)[rc:1,]
1987 14.38 7.75 15.29 17.85 7.92 17.43
1988 13.05 6.54 13.52 16.41 7.65 14.58
1989 11.19 5.48 11.19 14.27 7.10 13.18
1990 9.09 4.83 8.45 11.84 5.91 11.73
1991 7.54 4.83 6.86 9.90 4.92 9.59
1992 6.62 4.11 6.36 8.61 4.38 8.37
1993 4.47 2.83 5.13 6.35 3.16 5.20
1994 2.97 1.96 3.45 4.38 2.33 2.65
1995 2.28 1.96 2.69 2.49 1.73 1.78
1996 1.15 0.92 1.50 0.97 0.60 0.00
Now, can move the cumsum of 1 row? For obtain a this:
1986 14.38 7.75 15.29 17.85 7.92 17.43
1987 13.05 6.54 13.52 16.41 7.65 14.58
1988 11.19 5.48 11.19 14.27 7.10 13.18
1989 9.09 4.83 8.45 11.84 5.91 11.73
1990 7.54 4.83 6.86 9.90 4.92 9.59
1991 6.62 4.11 6.36 8.61 4.38 8.37
1992 4.47 2.83 5.13 6.35 3.16 5.20
1993 2.97 1.96 3.45 4.38 2.33 2.65
1994 2.28 1.96 2.69 2.49 1.73 1.78
1995 1.15 0.92 1.50 0.97 0.60 0.00
1996 0.00 0.00 0.00 0.00 0.00 0.00
thanks...
Alfredo
More information about the R-help
mailing list