Re: [R] inverse cumsum

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Wed, 18 Jun 2008 12:53:20 +0100

On Wed, 2008-06-18 at 12:37 +0200, Wacek Kusnierczyk wrote:
> Alfredo Alessandrini wrote:
> > I've a matrix like this:
> >
> > 1985 1.38 1.27 1.84 2.10 0.59 3.47
> > 1986 1.05 1.13 1.21 1.54 0.21 2.14
> > 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
> >
> > Can I utilise a cumsum inverse? from 1995 to 1985?
> >
> i guess you mean columnwise cumsums computed starting from the bottom up?

> 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,]

Alternatively, just use rev() instead of doing it by hand:

> dat <- as.data.frame(matrix(scan(), ncol = 7, byrow = TRUE))

1: 1985     1.38     1.27     1.84     2.10     0.59     3.47
8: 1986     1.05     1.13     1.21     1.54     0.21     2.14
15: 1987     1.33     1.21     1.77     1.44     0.27     2.85
22: 1988     1.86     1.06     2.33     2.14     0.55     1.40
29: 1989     2.10     0.65     2.74     2.43     1.19     1.45
36: 1990     1.55     0.00     1.59     1.94     0.99     2.14
43: 1991     0.92     0.72     0.50     1.29     0.54     1.22
50: 1992     2.15     1.28     1.23     2.26     1.22     3.17
57: 1993     1.50     0.87     1.68     1.97     0.83     2.55
64: 1994     0.69     0.00     0.76     1.89     0.60     0.87
71: 1995     1.13     1.04     1.19     1.52     1.13     1.78
78:
Read 77 items
> rownames(dat) <- as.character(dat[,1])
> dat <- dat[,-1]
> rc <- nrow(dat)
> cs1 <- apply(dat[rc:1,], 2, cumsum)[rc:1,]
> cs1

        V2 V3 V4 V5 V6 V7

1985 15.66 9.23 16.84 20.52 8.12 23.04
1986 14.28 7.96 15.00 18.42 7.53 19.57
1987 13.23 6.83 13.79 16.88 7.32 17.43
1988 11.90 5.62 12.02 15.44 7.05 14.58
1989 10.04 4.56  9.69 13.30 6.50 13.18
1990  7.94 3.91  6.95 10.87 5.31 11.73
1991  6.39 3.91  5.36  8.93 4.32  9.59
1992  5.47 3.19  4.86  7.64 3.78  8.37
1993  3.32 1.91  3.63  5.38 2.56  5.20
1994  1.82 1.04  1.95  3.41 1.73  2.65
1995  1.13 1.04  1.19  1.52 1.13  1.78

> cs2 <- apply(dat, 2, function(x) {rev(cumsum(rev(x)))})
> cs2

        V2 V3 V4 V5 V6 V7

1985 15.66 9.23 16.84 20.52 8.12 23.04
1986 14.28 7.96 15.00 18.42 7.53 19.57
1987 13.23 6.83 13.79 16.88 7.32 17.43
1988 11.90 5.62 12.02 15.44 7.05 14.58
1989 10.04 4.56  9.69 13.30 6.50 13.18
1990  7.94 3.91  6.95 10.87 5.31 11.73
1991  6.39 3.91  5.36  8.93 4.32  9.59
1992  5.47 3.19  4.86  7.64 3.78  8.37
1993  3.32 1.91  3.63  5.38 2.56  5.20
1994  1.82 1.04  1.95  3.41 1.73  2.65
1995  1.13 1.04  1.19  1.52 1.13  1.78

> all.equal(cs1, cs2)

[1] TRUE HTH G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help_at_r-project.org 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.
Received on Wed 18 Jun 2008 - 12:00:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 18 Jun 2008 - 14:31:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive