Hello everyone, I have the following matrix [,1] [,2] [,3] [,4] [1,] 0.002809706 0.0063856960 0.0063856960 0.011749681 [2,] 0.004893124 0.0023118418 -0.0005122951 -0.014646465 [3,] 0.003547897 0.0063355297 0.0030410542 0.011403953 [4,] 0.004838299 -0.0040383645 -0.0090406831 -0.011027569 [5,] 0.035648755 0.0334815590 0.0380977404 0.059817597 I want to apply a function on each column varying the first parameter of the function. If I do it on one column only with apply it works perfectly as follow: apply(column1, 1, myfunction, parameter1= ..., parameter2=...) But when I try to do it on each column without even varying the parameter it doesn't work: apply(matrix, 2, apply, MARGIN = 1, Fun = myfunction, parameter1 =..., parameter2=...) I get the following error: Error in FUN(newX[, i], ...) : unused argument(s) (2, function (X, MARGIN, FUN, ...) Does someone know how to solve this? and is there a way to send a vector of parameter1 instead of a scalar? thank you ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Applying-a-function-on-each-columns-of-a-matrix-tp1415660p1415660.html Sent from the R help mailing list archive at Nabble.com.
Carlos J. Gil Bellosta
2010-Jan-29 17:18 UTC
[R] Applying a function on each columns of a matrix
Hello, You could do something along the following lines: sapply( 1:ncol( my.matrix ), function( i ) my.foo( my.matrix[,i], my.parm[i] ) Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com anna wrote:> Hello everyone, I have the following matrix > [,1] [,2] [,3] [,4] > [1,] 0.002809706 0.0063856960 0.0063856960 0.011749681 > [2,] 0.004893124 0.0023118418 -0.0005122951 -0.014646465 > [3,] 0.003547897 0.0063355297 0.0030410542 0.011403953 > [4,] 0.004838299 -0.0040383645 -0.0090406831 -0.011027569 > [5,] 0.035648755 0.0334815590 0.0380977404 0.059817597 > > I want to apply a function on each column varying the first parameter of the > function. If I do it on one column only with apply it works perfectly as > follow: > apply(column1, 1, myfunction, parameter1= ..., parameter2=...) > But when I try to do it on each column without even varying the parameter it > doesn't work: > apply(matrix, 2, apply, MARGIN = 1, Fun = myfunction, parameter1 =..., > parameter2=...) > I get the following error: > Error in FUN(newX[, i], ...) : > unused argument(s) (2, function (X, MARGIN, FUN, ...) > Does someone know how to solve this? and is there a way to send a vector of > parameter1 instead of a scalar? thank you > > ----- > Anna Lippel
But then I would have to make a loop right? ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Applying-a-function-on-each-columns-of-a-matrix-tp1415660p1415743.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2010-Jan-29 18:26 UTC
[R] Applying a function on each columns of a matrix
See sweep function On Fri, Jan 29, 2010 at 2:32 PM, anna <lippelanna24 at hotmail.com> wrote:> > Hello everyone, I have the following matrix > ? ? ? ? ? ? [,1] ? ? ? ? ?[,2] ? ? ? ? ?[,3] ? ? ? ? [,4] > ?[1,] ?0.002809706 ?0.0063856960 ?0.0063856960 ?0.011749681 > ?[2,] ?0.004893124 ?0.0023118418 -0.0005122951 -0.014646465 > ?[3,] ?0.003547897 ?0.0063355297 ?0.0030410542 ?0.011403953 > ?[4,] ?0.004838299 -0.0040383645 -0.0090406831 -0.011027569 > ?[5,] ?0.035648755 ?0.0334815590 ?0.0380977404 ?0.059817597 > > I want to apply a function on each column varying the first parameter of the > function. If I do it on one column only with apply it works perfectly as > follow: > apply(column1, 1, myfunction, parameter1= ..., parameter2=...) > But when I try to do it on each column without even varying the parameter it > doesn't work: > apply(matrix, 2, apply, MARGIN = 1, Fun ?= myfunction, parameter1 =..., > parameter2=...) > I get the following error: > Error in FUN(newX[, i], ...) : > ?unused argument(s) (2, function (X, MARGIN, FUN, ...) > Does someone know how to solve this? and is there a way to send a vector of > parameter1 instead of a scalar? thank you > > ----- > Anna Lippel > -- > View this message in context: http://n4.nabble.com/Applying-a-function-on-each-columns-of-a-matrix-tp1415660p1415660.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O