Hello , I can not get apply function to do what I want when doing stuff on rows. Let's say I want to divide the rows of matrix by their mean, the below show you get the same result weather you use 1 or 2, i.e. same result for columns than for means..:( Thanks a lot for the help, m = matrix( c(1,4,7,4,5,8,7,8,9), nrow = 3 ) divideByMean = function( v ){ return( v/mean(v)) } rowMean = c( mean( m[1,]),mean( m[2,]),mean( m[3,]) ) rowMean colMean = c( mean( m[,1]),mean( m[,2]),mean( m[,3]) ) colMean m print("ByRow") apply( m, 1, divideByMean) print("ByCol") apply( m, 2, divideByMean) [[alternative HTML version deleted]]
The mean by col and by row are the same: colMeans(m) == rowMeans(m) So: m / rowMeans(m) # You don't need apply here m / colMeans(m) On Tue, Jun 1, 2010 at 2:26 PM, Joachim de Lezardiere < joachim.lezard@gmail.com> wrote:> Hello , > > > > I can not get apply function to do what I want when doing stuff on rows. > Let's say I want to divide the rows of matrix by their mean, the below show > you get the same result weather you use 1 or 2, i.e. same result for > columns > than for means..:( > > > > Thanks a lot for the help, > > > > > > > > m = matrix( c(1,4,7,4,5,8,7,8,9), nrow = 3 ) > > > > divideByMean = function( v ){ > > return( v/mean(v)) > > } > > > > rowMean = c( mean( m[1,]),mean( m[2,]),mean( m[3,]) ) > > rowMean > > > > colMean = c( mean( m[,1]),mean( m[,2]),mean( m[,3]) ) > > colMean > > > > m > > print("ByRow") > > apply( m, 1, divideByMean) > > > > print("ByCol") > > apply( m, 2, divideByMean) > > > > > > > > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Well, your example matrix is symmetric, so row and column operations naturally return the same values. You may want to note though that if you apply your function to a matrix along rows, the results will be stored in the __columns__ of the resulting matrix. Thus, if you want to simply divide the rows of a matrix by their mean, you need to transpose the result. Example: m = matrix(c(0,1,2,5), 2,2) m apply( m, 1, divideByMean) t(apply( m, 1, divideByMean)) # This is what you probably want. HTH, Peter On Tue, Jun 1, 2010 at 10:26 AM, Joachim de Lezardiere < joachim.lezard@gmail.com> wrote:> Hello , > > > > I can not get apply function to do what I want when doing stuff on rows. > Let's say I want to divide the rows of matrix by their mean, the below show > you get the same result weather you use 1 or 2, i.e. same result for > columns > than for means..:( > > > > Thanks a lot for the help, > > > > > > > > m = matrix( c(1,4,7,4,5,8,7,8,9), nrow = 3 ) > > > > divideByMean = function( v ){ > > return( v/mean(v)) > > } > > > > rowMean = c( mean( m[1,]),mean( m[2,]),mean( m[3,]) ) > > rowMean > > > > colMean = c( mean( m[,1]),mean( m[,2]),mean( m[,3]) ) > > colMean > > > > m > > print("ByRow") > > apply( m, 1, divideByMean) > > > > print("ByCol") > > apply( m, 2, divideByMean) > > > > > > > > > [[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]]