[R] using matrix data for function

Brian.J.GREGOR@odot.state.or.us Brian.J.GREGOR at odot.state.or.us
Fri Sep 19 19:13:36 CEST 2003


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




More information about the R-help mailing list