title: Tidyquant rolling sharpe ratio using dynamic risk free rate
body: <p>I'm looking to pass the risk free rate dynamically into the PortfolioAnalytics function SharpeRatio.annualized through the mutated PA function rollapply by using tq_mutate. </p>
  
  <p>Edit* At the moment I am unable to roll the time period window of risk free rate along with the portfolio either through a group or column. Example window would be 120 months and would extend for both returns and the risk free rate. As it is written below, I can substitute "Rf =" for a number or equation but not the rolling period. How can I include this rolling risk free rate in the below function?</p>
  
  <p>I have a tibble of monthly time series returns, with one group ("portfolio") containing the RFR set up that looks like this, I also included the RFR as a column "return.rf" to see if that could work as well.</p>
  
  <p><em>Note Sharpes in the tibble below do not have RF subtracted</em></p>
  
  <pre><code># A tibble: 2,133 x 13
  # Groups:   portfolio [9]
     portfolio index         return return.rf Sharpe_12 Sharpe_36 Sharpe_60
     &lt;chr&gt;     &lt;date&gt;         &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;
   1 BH        1998-04-30 -0.0142     0.00430        NA        NA        NA
   2 BH        1998-05-29  0.000259   0.00400        NA        NA        NA
   3 BH        1998-06-30 -0.0525     0.00410        NA        NA        NA
   4 BH        1998-07-31 -0.00949    0.00400        NA        NA        NA
   5 BH        1998-08-31  0.000536   0.00430        NA        NA        NA
   6 BH        1998-09-30  0.0486     0.00460        NA        NA        NA
   7 BH        1998-10-30  0.00685    0.00320        NA        NA        NA
   8 BH        1998-11-30 -0.00452    0.00310        NA        NA        NA
   9 BH        1998-12-31  0.00615    0.00380        NA        NA        NA
  10 BH        1999-01-29  0.0323     0.00350        NA        NA        NA
  </code></pre>
  
  <p>My code is functioning if I comment out the pass through of Rf:</p>
  
  <pre><code>tidy_opt_roll_returns &lt;- tidy_opt.returns %&gt;%    
    tq_mutate(
      # tq_mutate args
      select     = return,
      mutate_fun = rollapply, 
      # rollapply args
      width      = 120,
      align      = "right",
      FUN        = SharpeRatio.annualized,
      # mean args ## commented out next three lines ##
      # Rf         = return.rf,
      # tq_mutate args
      col_rename = "Sharpe_120")
  </code></pre>
  
  <p>Thanks!</p>