On 21-05-2013, at 09:16, Suparna Mitra <suparna.mitra...@gmail.com> wrote:
> Hello R Experts, > I need a bit of help in using loop. > I have a matrix onto which I need to use several functions. > > In a simplified form suppose my matrix is >> mdat > [,1] [,2] [,3] [,4] [,5] > [1,] 1 2 3 4 5 > [2,] 11 12 13 10 10 > [3,] 2 3 4 5 6 > [4,] 10 9 8 9 10 > [5,] 5 6 7 8 4 > > And I have one vector >> x > [1] 14 15 10 11 18 > > Now suppose in simple form I want to create a matrix in which each col > value will be divided with consecutive no from vector x. For example > > column 1 of new vector will be C1=mdat[,1]*100/x[1] > >> C1 > [1] 7.142857 78.571429 14.285714 71.428571 35.714286 > > Now how can I use the loop to have the complete vector at a time? > I tried something like this, but in vain. > for(i in 1:5) { > Data=(mdat[,i]*100/x[i], add=T) > } > See this discussion: http://r.789695.n4.nabble.com/Divide-matrix-columns-by-different-numbers-td4666013.html Other possibilities taken from that thread mdat*100/rep(x,each=nrow(mdat)) 100*t(t(mdat)/x) Berend ______________________________________________ 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.