apply the function for each row and use the columns as arguments

advertisements

I am sorry if this question has been asked before, but could not find a solution to my problem. Which apply-like function fits the below case?

I have an R function which has 3 arguments (x, y, z). What it does is basically to call an PostgreSQL function which quires x, y, z and retrieve a dataframe consisting of corresponding values a, b, c, something like this;

myfunc <- function(x, y, z){
  get.df <- fn$sqldf("SELECT * FROM retrieve_meta_data('$x', '$y', '$z')")
# get.df is a dataframe consisting of x number of rows and columns a, b, c
# some dataframe manipulation...
  return(get.df)
}

What I am looking for is to call this function by using a dataframe (call.df) with x number of rows and columns x, y, z. So apply the function for each ith row and using the columns as arguments.

I have looked through a range of apply-like functions but I have failed so far. It's probably way easy.

I imagine something like apply(call.df, c(1,2), myfunc) but this gives the error;

Error in eval(expr, envir, enclos) :
argument "y" is missing, with no default

I hope am clear enough without supplying any dummy data. Any pointers would be very much appreciated, thanks!


If x, y, and z are the first three columns of df, then this should work:

apply(df,1,function(params)myfunc(params[1],params[2], params[3]))

apply(df,1,FUN) takes the first argument, df, and passes it to FUN row-wise (because the second argument is 1). So in function(params), params is a row of df. Hence, params[1] is the first column of that row, etc.