2

I want to use the rows of a dataframe as arguments for a function. I know this can be done like this:

 arg <- expand.grid(n = 100, mean = -1:1, sd = 0:3)

 apply(arg, 1, FUN = function(x) rnorm(x[1], x[2], x[3]))

I would like, however, to have a more general, elegant way to pass the arguments using the column names of a dataframe to specify them (if the colum names is not an option, the order of the columns will do it as well).

I don't want to specify in every function which column refers to each argument, as I have done in the example above. Also, I would like to pass the values to functions with arbitrary number of arguments.

D Pinto
  • 871
  • 9
  • 27

1 Answers1

1

We can use Map

do.call(Map, c(f= rnorm, arg))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • that worked fine! but now I stumbled in the situation where on of the arguments is a dataframe (the data I am going to use to estimate the model, for example). is there a work around for this? – D Pinto Feb 06 '17 at 20:39
  • @DPinto Please post as a new question – akrun Feb 07 '17 at 03:24
  • I have posted a new question. Here the [link](http://stackoverflow.com/questions/42112654/r-pass-rows-of-a-data-frame-as-parameters-to-a-function-while-keeping-other-arg) – D Pinto Feb 08 '17 at 12:12