zhaoxing731
2011-Jan-11 05:17 UTC
[R] how to use "apply" function partial to each vector of a matrix
Hello Suppose I have a matrix mat=(1:16,2) [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 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 the 1st column #sum the 1st 3 numbers of the 2nd column #sum the 1st number of the 3rd column #sum the 1st 3 numbers of the 4th column #they are specified by vector end a for loop for (i in 1:4) { outsum=sum[1:end[i],i] } but when I want to do it to a large dataset, it's inefficency becomes a problem, so need vectorization the "apply" function will sum all of each vector, which is not my purpose Thank you in advance Yours sincerely ZhaoXing Department of Health Statistics West China School of Public Health Sichuan University No.17 Section 3, South Renmin Road Chengdu, Sichuan 610041 P.R.China __________________________________________________ ?????????????????????????????
Michael Bedward
2011-Jan-11 07:42 UTC
[R] how to use "apply" function partial to each vector of a matrix
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 On 11 January 2011 16:17, zhaoxing731 <zhaoxing731 at yahoo.com.cn> wrote:> Hello > > Suppose I have a matrix mat=(1:16,2) > [,1] [,2] [,3] [,4] > [1,] 1 5 9 13 > [2,] 2 6 10 14 > [3,] 3 7 11 15 > [4,] 4 8 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 the 1st column > #sum the 1st 3 numbers of the 2nd column > #sum the 1st number of the 3rd column > #sum the 1st 3 numbers of the 4th column > #they are specified by vector end > a for loop > for (i in 1:4) > { > outsum=sum[1:end[i],i] > } > but when I want to do it to a large dataset, it's inefficency becomes a problem, so need vectorization > the "apply" function will sum all of each vector, which is not my purpose > > Thank you in advance > > Yours sincerely > > > > > ZhaoXing > Department of Health Statistics > West China School of Public Health > Sichuan University > No.17 Section 3, South Renmin Road > Chengdu, Sichuan 610041 > P.R.China > > > __________________________________________________ > ??????????????? > > > ______________________________________________ > R-help at 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. > >
Apparently Analagous Threads
- how to coerce part of each column of a matrix to a vector and merge them
- matrices call a function element-wise
- How can we make a vector call a function element-wise efficiently?
- how to return multipy matrix in a function
- how to improve the precison of this calculation?