I am looking the *right* R idiom to run a function over a set of parameters and create a long data frame from the results. Imagine that you have the following toy function:

```
fun <- function(sd, mean, foobar = "foobar") {
  list(random = rnorm(10) * sd + mean + 1:10, foobar = foobar)
}

```

Now you want to run `fun` over different values of `sd` and `mean`:

```
par_sd <- rep(1:5, 3)
par_mean <- rep(0:2, each = 5)
pars <- data.frame(sd = par_sd, mean = par_mean)

```

I want to run `fun` for the parameters in each row of `pars`, and collect the results in a data frame with columns `sd`, `mean`, `pos`, `value`. Here is a rather clumsy solution:

```
set.seed(42)

## Run fun
res <- lapply(seq_len(nrow(pars)), function(x) {
  do.call(fun, as.list(pars[x, ]))
})

## Select the result we need
res <- lapply(res, "[[", "random")

## Make it a single data frame
res <- do.call(rbind, res)

## Together with the parameters
res <- as.data.frame(cbind(sd = par_sd, mean = par_mean, res))
colnames(res) <- c("sd", "mean", 1:10)

## Make it a long data frame
res <- reshape2::melt(res, id.vars=c("sd", "mean"), 
         variable.name = "pos", value.name="value")

## Done
res[1:5,]
#>   sd mean pos      value
#> 1  1    0   1 2.37095845
#> 2  2    0   1 3.60973931
#> 3  3    0   1 0.08008422
#> 4  4    0   1 2.82180049
#> 5  5    0   1 2.02999300

```

Is there a simpler way to do this? Anyone knows a package that does things like this? My quick search did not give any good results...