[R] summing up a matrix

David L Carlson dcarlson at tamu.edu
Fri Nov 18 16:42:53 CET 2016


You should read some of the free tutorials about R. Also use dput() to send your data and plain text (no html) emails.

> dput(mtx)
structure(c(20L, 100L, 50L, 200L, 200L, 200L, 100L, 100L, 100L, 
50L, 50L, 50L, 100L, 100L, 100L, 30L, 30L, 30L), .Dim = c(3L, 
6L), .Dimnames = list(c("2016-11", "2016-12", "2017-01"), NULL))
> t(apply(mtx, 1, cumsum))
        [,1] [,2] [,3] [,4] [,5] [,6]
2016-11   20  220  320  370  470  500
2016-12  100  300  400  450  550  580
2017-01   50  250  350  400  500  530

Use ?dput, ?t, ?apply, and ?cumsum to read the manual pages for these functions.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matthias Weber
Sent: Friday, November 18, 2016 9:16 AM
To: 'r-help at r-project.org'
Subject: [R] summing up a matrix

Hello together,

is it possible, to summing up a matrix?
I have the following matrix at the moment:

                                [,1]         [,2]         [,3]        [,4]         [,5]         [,6]
2016-11                20            200        100         50           100         30
2016-12                100          200        100         50           100         30
2017-01                50            200        100         50           100         30

Now I want to summing up the matrix in a new matrix.

The result should look like the following:

                                [,1]         [,2]         [,3]        [,4]         [,5]         [,6]
2016-11                20            220        320         370         470         500
2016-12                100          300        400         450         550         580
2017-01                50            250        350         400         500         530

Is it possible, to create that?

Thanks for your help.

Best regards.

Mat


	[[alternative HTML version deleted]]

______________________________________________
R-help at 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.



More information about the R-help mailing list