R apply function returning multiple values to each row of data frame and add these as new columns to the data frame
<p>Say I have a data frame which is a cross product of the sequence of 1 to 20 with itself:</p>

<pre><code>a &lt;- seq(1,20,1)
combis &lt;- expand.grid(a,a)
colnames(combis) &lt;- c("DaysBack","DaysForward")
</code></pre>

<p>So the data looks like:</p>

<pre><code>DaysBack  DaysForward
1         1
2         1
...
19        20
20        20
</code></pre>

<p>I want to apply a function which takes the days back, and days forward, and returns several values, and then adds these as columns to this data frame. So my function would look something like:</p>

<pre><code>## operation to apply on each row
do_something &lt;- function(days_back, days_forward)
{
    # logic to work out some values
    ...
    # return those values
    c(value_1, value_2, value_3)
}
</code></pre>

<p>And then to add this to the original data frame, so "combis" should for example look like:</p>

<pre><code>DaysBack  DaysForward  Value1  Value2  Value3
1         1            5       6       7
2         1            4       2       3
...
19        20           1       9       3
20        20           2       6       8
</code></pre>

<p>How do I do this and get back a data frame.</p>

<p><strong>EDIT:</strong></p>

<p>My <strong>do_something</strong> function currently operates on two values, <strong>days_back</strong> and <strong>days_forward</strong>. It uses these in the context of another dataframe called <strong>pod</strong>, which (for this example) looks something like:</p>

<pre><code>Date          Price
2016-01-01    3.1
2016-01-02    3.33
...
2016-04-12    2.12
</code></pre>

<p>Now say i pass in <strong>days_back=1</strong> and <strong>days_forward=2</strong>, what I do is for each row i find the price 1 day back, and the price 2 days forward, and I add this as a column called <strong>Diff</strong> to the data. I do this by adding lead/lag columns as appropriate (i found <strong>shift</strong> code to do this here <a href="https://stackoverflow.com/questions/28432599/lag-in-r-vector-dataframe">What&#39;s the opposite function to lag for an R vector/dataframe?</a>), so I'm not doing any looping. Once I have the differences per row, I calculate the mean and standard deviation of <strong>Diff</strong> and return these two values. I.e. for combination <strong>days_back=1</strong> and <strong>days_forward=2</strong> I have some mean and sd of the diff. Now I want this for all combinations of <strong>days_back</strong> and <strong>days_forward</strong> with each ranging from 1 to 20. In the example data i gave when i first asked the question, <strong>mean_diff</strong> would correspond to <strong>Value1</strong> and <strong>sd_diff</strong> would correspond to <strong>Value2</strong> for example</p>

<p>So to be clear, currently my <strong>do_something</strong> operates directly on two values and not on two sets of column vectors. I'm sure it can be re-written to operate on two vectors, but then again I have the same issue in that I don't know how to return this data so that in the end I get a data frame that looks like what I showed above as my target output. </p>

<p>Thanks </p>