title: r rolling custom function over 2 columns
body: <p>I would like to roll a customized function, that uses two data columns, over a data frame. I see how to do this with one data column, but I can't quite wrangle two. (The real data frame is much larger.)</p>
  <pre><code>my_df &lt;- data.frame(&quot;id&quot;=c(&quot;151&quot;, &quot;143&quot;, &quot;199&quot;, &quot;122&quot;, &quot;156&quot;), 
                  &quot;person&quot;=c(&quot;mother&quot;, &quot;father&quot;, &quot;grandma&quot;, &quot;child&quot;, &quot;sister&quot;, &quot;mother&quot;, &quot;grandma&quot;, &quot;grandma&quot;, &quot;father&quot;, &quot;mother&quot;,&quot;mother&quot;, &quot;mother&quot;, &quot;grandma&quot;, &quot;child&quot;, &quot;sister&quot;, &quot;mother&quot;, &quot;mother&quot;, &quot;grandma&quot;, &quot;father&quot;, &quot;mother&quot;, &quot;mother&quot;, &quot;mother&quot;, &quot;mother&quot;, &quot;mother&quot;, &quot;mother&quot;))
  
  my_new_df &lt;- my_df %&gt;%
  group_by(id) %&gt;% # first I subset by ID number
  mutate(total = n()) # calculate the total number of observations per ID
  filter(person=='mother') %&gt;% # then I filter the observations I want to know about
  mutate(n_mother = n()) %&gt;% calculate the # of 'mother' observations per ID
  mutate(prop_mother = rollapply(n_mother/total, width=1, FUN=(??)) # Here I get stuck - I want the proportion of 'mother' observations updated for every observation from this ID number
  </code></pre>
  Do I write a custom function to call within the pipe?
  <pre><code>calculate_mother = function(n_mother){
     return(n_mother / total)
  }
  </code></pre>
  After this, I want to calculate the rolling mean and variance of <code>prop_mother</code> as well, but I can't do that until I actually calculate <code>prop_mother</code>