Hi there,

I try to calculate the cumsum of row and column of a matrix as follows.

> m <- matrix(1:10, ncol = 2)
> m
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10
> apply(m, 1, cumsum)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    7    9   11   13   15
> apply(m, 2, cumsum)
     [,1] [,2]
[1,]    1    6
[2,]    3   13
[3,]    6   21
[4,]   10   30
[5,]   15   40

My question is why the dim of the return value of apply(m, 1, cumsum) is not 5x2, but 2x5.

Best,

Jinsong

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to