Hi All, I have a function, f(x,y) I have a matrix of data, m, with the 1st column is x and the 2nd column is y What's the best way to get f(x,y) for each row of the matrix? I tried result<-f(m[,1],m[,2]) but it doesn't work. Thanks! Bing --------------------------------- 1060 Commerce Park Oak Ridge National Laboratory P.O. Box 2008, MS 6480 Oak Ridge, TN 37831-6480 Phone: 865-241-0761 Email: zhangb at ornl.gov
Thanks. How about I have a third parameter for the function, which is a fixed object? i.e. the function is f(o,x,y) Bing>===== Original Message From james.holtman at convergys.com ====>Assuming that f(x,y) is not vectorize, try > >apply(your.matrix, 1, function(x) f(x[1], x[2])) > >as in: > >> x.1 <- matrix(1:12,ncol=2) >> x.1 > [,1] [,2] >[1,] 1 7 >[2,] 2 8 >[3,] 3 9 >[4,] 4 10 >[5,] 5 11 >[6,] 6 12 >> x.f <- function(x,y) x+y >> apply(x.1, 1, function(x) x.f(x[1], x[2])) >[1] 8 10 12 14 16 18 >> >__________________________________________________________ >James Holtman "What is the problem you are trying to solve?" >Executive Consultant -- Office of Technology, Convergys >james.holtman at convergys.com >(513) 723-2929 > > > > Bing Zhang > <bih at ornl.gov> To: r-help<r-help at stat.math.ethz.ch>> Sent by: cc: > r-help-bounces at stat.m Subject: [R] using matrixdata for function> ath.ethz.ch > > > 09/17/2003 14:02 > > > > > > >Hi All, > >I have a function, f(x,y) >I have a matrix of data, m, with the 1st column is x and the 2nd column is >y >What's the best way to get f(x,y) for each row of the matrix? >I tried >result<-f(m[,1],m[,2]) but it doesn't work. > >Thanks! > >Bing > >--------------------------------- >1060 Commerce Park >Oak Ridge National Laboratory >P.O. Box 2008, MS 6480 >Oak Ridge, TN 37831-6480 >Phone: 865-241-0761 >Email: zhangb at ornl.gov > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > >-- >"NOTICE: The information contained in this electronic mail transmission is >intended by Convergys Corporation for the use of the named individual or >entity to which it is directed and may contain information that is >privileged or otherwise confidential. If you have received this electronic >mail transmission in error, please delete it from your system without >copying or forwarding it, and notify the sender of the error by reply email >or by telephone (collect), so that the sender's address records can be >corrected."--------------------------------- 1060 Commerce Park Oak Ridge National Laboratory P.O. Box 2008, MS 6480 Oak Ridge, TN 37831-6480 Phone: 865-241-0761 Email: zhangb at ornl.gov
Don't think this is "best", but here's one way:> mat <- matrix(1:12, 6) > mat[,1] [,2] [1,] 1 7 [2,] 2 8 [3,] 3 9 [4,] 4 10 [5,] 5 11 [6,] 6 12> f <- function(x, y) x + y > apply(mat, 1, function(x) do.call("f", as.list(x)))[1] 8 10 12 14 16 18 Note that apply(mat, 1, f) won't work, because both values are passed to f in a single vector. Perhaps better alternatives are: 1. Re-write f so that it takes a single vector of two elements, or write a wrapper fw <- function(x) f(x[1], x[2]), then use fw in apply(). 2. Re-write f so that it's vectorized, so that f(mat[,1], fmat[,2]) works. HTH, Andy> -----Original Message----- > From: Bing Zhang [mailto:bih at ornl.gov] > Sent: Wednesday, September 17, 2003 2:03 PM > To: r-help > Subject: [R] using matrix data for function > > > Hi All, > > I have a function, f(x,y) > I have a matrix of data, m, with the 1st column is x and the > 2nd column is y What's the best way to get f(x,y) for each > row of the matrix? I tried > result<-f(m[,1],m[,2]) but it doesn't work. > > Thanks! > > Bing > > --------------------------------- > 1060 Commerce Park > Oak Ridge National Laboratory > P.O. Box 2008, MS 6480 > Oak Ridge, TN 37831-6480 > Phone: 865-241-0761 > Email: zhangb at ornl.gov > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >
On Thu, 2003-09-18 at 06:02, Bing Zhang wrote:> Hi All, > > I have a function, f(x,y) > I have a matrix of data, m, with the 1st column is x and the 2nd column is y > What's the best way to get f(x,y) for each row of the matrix? > I tried > result<-f(m[,1],m[,2]) but it doesn't work.That is the best way, provided your function handles vectors nicely. Apparently, yours doesn't. Next best is something like apply(m,2,f) Cheers Jason -- Indigo Industrial Controls Ltd. http://www.indigoindustrial.co.nz +64-(0)21-343-545
I think you will find the "new" (1.7.0) function mapply(sum, mat[,1], mat[,2]) is an excellent generic approach (though in the particular example, rowsum() would do the job). I'd been waiting years for mapply(), love it!> -----Original Message----- > From: Liaw, Andy [mailto:andy_liaw at merck.com] > Sent: 17 September 2003 19:47 > To: 'Bing Zhang'; r-help > Subject: RE: [R] using matrix data for function > > > Security Warning: > If you are not sure an attachment is safe to open please contact > Andy on x234. There are 0 attachments with this message. > ________________________________________________________________ > > Don't think this is "best", but here's one way: > > > mat <- matrix(1:12, 6) > > mat > [,1] [,2] > [1,] 1 7 > [2,] 2 8 > [3,] 3 9 > [4,] 4 10 > [5,] 5 11 > [6,] 6 12 > > f <- function(x, y) x + y > > apply(mat, 1, function(x) do.call("f", as.list(x))) > [1] 8 10 12 14 16 18 > > Note that apply(mat, 1, f) won't work, because both values > are passed to > f > in a single vector. > > Perhaps better alternatives are: > > 1. Re-write f so that it takes a single vector of two > elements, or write > a > wrapper fw <- function(x) f(x[1], x[2]), then use fw in apply(). > > 2. Re-write f so that it's vectorized, so that f(mat[,1], fmat[,2]) > works. > > HTH, > Andy > > > > -----Original Message----- > > From: Bing Zhang [mailto:bih at ornl.gov] > > Sent: Wednesday, September 17, 2003 2:03 PM > > To: r-help > > Subject: [R] using matrix data for function > > > > > > Hi All, > > > > I have a function, f(x,y) > > I have a matrix of data, m, with the 1st column is x and the > > 2nd column is y What's the best way to get f(x,y) for each > > row of the matrix? I tried > > result<-f(m[,1],m[,2]) but it doesn't work. > > > > Thanks! > > > > Bing > > > > --------------------------------- > > 1060 Commerce Park > > Oak Ridge National Laboratory > > P.O. Box 2008, MS 6480 > > Oak Ridge, TN 37831-6480 > > Phone: 865-241-0761 > > Email: zhangb at ornl.gov > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
It seems to me that the simplest approach is as follows. Say you have a function as follows: dosomething <- function(x,y) 0.523*x^2 + 0.34*y Then you just use apply as follows to get your result (assuming that the first column of matrix m contains the x values and the second column contains the y values: apply(m, 1, function(x) dosomething(x[1], x[2])) Brian Gregor, P.E. Transportation Planning Analysis Unit Oregon Department of Transportation Brian.J.GREGOR at odot.state.or.us (503) 986-4120 -----Original Message-----> From: Bing Zhang [mailto:bih at ornl.gov] > Sent: Wednesday, September 17, 2003 2:03 PM > To: r-help > Subject: [R] using matrix data for function > > > Hi All, > > I have a function, f(x,y) > I have a matrix of data, m, with the 1st column is x and the > 2nd column is y What's the best way to get f(x,y) for each > row of the matrix? I tried > result<-f(m[,1],m[,2]) but it doesn't work. > > Thanks! > > Bing