Hi:

On Tue, Jan 26, 2010 at 1:19 PM, jim holtman <jholtman@gmail.com> wrote:

> You can do something like this:
>
> lapply(1:nrow(X), function(.indx, param){
>    X[.indx,] * param[.indx]  # apply param[i] to row i of X
> }, param=arrayOf Params)
>
>
>
As a follow-up to Jim's reply, another solution to the problem where the
rows
of a matrix comprise the arguments of a function to be multiply applied is
to
use the mdply function in the plyr package, a nice convenience wrapper for
the
mapply() function. Here is a simple, reproducible example...

f <- function(a, b, c, x)  a * x^2 + b * x + c
df <- data.frame(a = c(1, 2, 1, 2), b = c(0, 3, 2, 0), c = c(0, 2, 1, 3),
                        x = c(1, 4, 5, 2))

df is a data frame whose columns contain the arguments of the function,
in order of appearance. When executed in mdply, each row is input as
argument to the function:

library(plyr)
mdply(df, f)   # Arg 1: data frame of parameters; arg 2: function name

My #&$*% gmail editor won't let me type in the result without discarding
everything else above it, but when you try it you'll see that it works. IMO,
this is one of the hidden gems of the plyr package.

HTH,
Dennis

	[[alternative HTML version deleted]]

