Hi. It would be easier to help you if you show us the result you are trying to obtain.
Here are my attempts: > sweep(a,2,div, FUN="/") [,1] [,2] [,3] [1,] 1 2.0 2.333333 [2,] 2 2.5 2.666667 [3,] 3 3.0 3.000000 or > a/matrix(rep(div,3),ncol=3, byrow=TRUE) [,1] [,2] [,3] [1,] 1 2.0 2.333333 [2,] 2 2.5 2.666667 [3,] 3 3.0 3.000000 Hope this helps. Andrija On Thu, Apr 18, 2013 at 7:20 AM, Sachinthaka Abeywardana < sachin.abeyward...@gmail.com> wrote: > Hi All, > > I have the following problem (read the commented bit below): > > a<-matrix(1:9,nrow=3) > > > a > > [,1] [,2] [,3] > [1,] 1 4 7 > [2,] 2 5 8 > [3,] 3 6 9 > > > div<-1:3 > > apply(a,2,function(x)x/div) ##want to divide each column by div- > instead each row is divided## > > > [,1] [,2] [,3] > [1,] 1 4.0 7 > [2,] 1 2.5 4 > [3,] 1 2.0 3 > > > apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does > something completele weird## [,1] [,2] [,3] > [1,] 1.000000 2.000000 3 > [2,] 2.000000 2.500000 3 > [3,] 2.333333 2.666667 3 > > > Any thoughts? > > > Thanks, > > Sachin > > [[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. > [[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.