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]]
Dear Matthias It is rather hard to read that since you posted in HTML which scrambles things but I think ?cumsum may help and possibly ?apply as you seem to want to work by rows. On 18/11/2016 15:16, Matthias Weber wrote:> 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
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.
Hi,
that should do:
mat <- rbind(c(20,200,100,50,100,30),
c(100,200,100,50,100,30),
c(50,200,100,50,100,30))
rownames(mat) <-
c("2016-11","2016-12","2017-01")
t(apply(mat,1,cumsum))
Best,
Christian
Am 18.11.2016 um 16:16 schrieb Matthias Weber:> 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.
>