Hi, Try: A<- structure(c(1, 2, 1, 2, 2, 1, 2, 1), .Dim = c(2L, 4L)) B<- structure(c(3, 1, 4, 1, 2, 2, 1, 2), .Dim = c(2L, 4L)) B1<- matrix(0,nrow(B),ncol(B)) B1[A==1]<-B[A==1] rowSums(B1) #[1] 7 4 A.K.
----- Original Message ----- From: tobias schlager <tobebry...@me.com> To: "r-help@r-project.org" <r-help@r-project.org> Cc: Sent: Thursday, September 26, 2013 5:51 PM Subject: [R] Sums based on values of other matrix Dear all, I have a big problem: - I got two matrices, A and B - A shows identifies the value of B, however the values of B must be summed - For instance, 1 1 2 2 2 2 1 1 gives matrix a 3 4 2 1 1 1 2 2 gives matrix b Now the result for the value 1 would be 7 4 which are the rowsums of the values of matrix B given that matrix A has the value 1. How can I do this automatically? I am really puzzled here. Thanks for your help guys, Tobi [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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.