Hi, I have a matrix with two columns. The first column means "indexes", the second one contents of those indexes. If I have a MATRIX like this,> MATRIX[,1] [,2] [1,] 1 3 [2,] 5 1 [3,] 2 1 [4,] 1 5 I'd like to get as a result vector the sums of these indexes, something like this:> c(8,1,0,0,1)How to do this? I did solved it this way, but is there some more elegant way: RESULTVECTOR=c(); RESULTMATRIX=c(); INDEXES=as.integer(names(table(TRANSP_TABLE[,1]))); for(i in INDEXES) { RESULTVECTOR=c(i,sum(MATRIX[,2][MATRIX[,1]==i])) RESULTMATRIX=rbind(RESULTMATRIX,RESULTVECTOR) } row.names(RESULTMATRIX)<-INDEXES; RESULTMATRIX=RESULTMATRIX[,2];> RESULTMATRIX1 2 5 8 1 1 Atte Tenkanen University of Turku, Finland
> x <- matrix(c(1,5,2,1,3,1,1,5), ncol=2) > > x[,1] [,2] [1,] 1 3 [2,] 5 1 [3,] 2 1 [4,] 1 5> (y <- tapply(x[,2], x[,1], sum))1 2 5 8 1 1> result <- numeric(max(as.numeric(names(y)))) > result[1] 0 0 0 0 0> result[as.numeric(names(y))] <- y > result[1] 8 1 0 0 1>On 8/19/06, Atte Tenkanen <attenka@utu.fi> wrote:> > Hi, > > I have a matrix with two columns. The first column means "indexes", the > second one contents of those indexes. If I have a MATRIX like this, > > > MATRIX > [,1] [,2] > [1,] 1 3 > [2,] 5 1 > [3,] 2 1 > [4,] 1 5 > > I'd like to get as a result vector the sums of these indexes, something > like this: > > > c(8,1,0,0,1) > > How to do this? > > I did solved it this way, but is there some more elegant way: > > RESULTVECTOR=c(); > RESULTMATRIX=c(); > INDEXES=as.integer(names(table(TRANSP_TABLE[,1]))); > > for(i in INDEXES) > { > RESULTVECTOR=c(i,sum(MATRIX[,2][MATRIX[,1]==i])) > RESULTMATRIX=rbind(RESULTMATRIX,RESULTVECTOR) > } > row.names(RESULTMATRIX)<-INDEXES; > RESULTMATRIX=RESULTMATRIX[,2]; > > > RESULTMATRIX > 1 2 5 > 8 1 1 > > > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
> x <- cbind(index=c(1,5,2,1), contents=c(3,1,1,5)) > xindex contents [1,] 1 3 [2,] 5 1 [3,] 2 1 [4,] 1 5 ## use tapply to get the values you want> z0 <- tapply(x[,"contents"], x[,"index"], sum) ## read ?tapply > z01 2 5 8 1 1 ## more work is needed to get them into the structure you want> r <- range(x[,"index"]) > r[1] 1 5> nn <- seq(r[1], r[2]) > nn[1] 1 2 3 4 5> z <- nn*0 > z[1] 0 0 0 0 0> names(z) <- nn > z1 2 3 4 5 0 0 0 0 0> z[names(z0)] <- z0 ## read about subscripting ?"[" > z1 2 3 4 5 8 1 0 0 1>## R is a matrix and vector language. Loops are rarely needed. ## Read "An Introduction to R". ## It is clickable from the Help menu in the Windows RGui Console. ## It is available in R-2.3.1/doc/manual/R-intro.pdf on all platforms. This is essentially the same as jim holtman's answer. I did some extra work to get nice names on the result vector.