Re: [R] how to use "apply" function partial to each vector of a matrix

2011-01-10 Thread Michael Bedward
Hello, Here is one way... m <- matrix(1:16, nrow=4) end <- c(2, 3, 1, 3) ii <- cbind(sequence(end), rep(1:length(end), end)) sums <- tapply(m[ ii ], ii[ , 2], sum) And here is another way... sums <- mapply(function(col, lastrow) sum(m[1:lastrow, col]), 1:ncol(m), end) Hope this helps, Michael

[R] how to use "apply" function partial to each vector of a matrix

2011-01-10 Thread zhaoxing731
Hello Suppose I have a matrix mat=(1:16,2) [,1] [,2] [,3] [,4] [1,]159 13 [2,]26 10 14 [3,]37 11 15 [4,]48 12 16 I just want to use the "apply" function to the matrix partially there is a vector end=c(2,3,1,3) #sum the 1st 2 numbers of