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
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 [EMAIL PROTECTED] =
Assuming that f(x,y) is not vectorize, try
apply(your.matrix, 1, function(x) f(x[1], x[2]))
as in:
x.1 -
Don't think this is best, but here's one way:
mat - matrix(1:12, 6)
mat
[,1] [,2]
[1,]17
[2,]28
[3,]39
[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
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
On Thu, 2003-09-18 at 06:41, Bing Zhang wrote:
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)
1) My earlier reply had a typo. Should've been apply(m,1,f).
2) Luckily, the answer to this question, and any more you're likely