title: Rolling correlation with id and date
body: <p>I have some data that has a name, date, and two factors (x,y).  I would like to calculate </p>
  
  <pre><code>  dt&lt;-seq(as.Date("2013/1/1"), by = "days", length.out = 20)
    df1&lt;-data.frame("ABC",dt,rnorm(20, 0,3),rnorm(20, 2,4) )
        names(df1)&lt;-c("name","date","x","y")
    df2&lt;-data.frame("XYZ",dt,rnorm(20, 2,5),rnorm(20, 3,10) )
        names(df2)&lt;-c("name","date","x","y")
    df&lt;-rbind(df1,df2)
  </code></pre>
  
  <p>I would like to add a column named "Correl" that for each date, takes the correlation of the previous 5 periods.  However, when the name changes, I would like it to give NA's instead.</p>
  
  <p>As you can see below, when the data becomes XYZ instead of ABC, the first 4 periods, the correlation is NA.  When there's 5 data points is when the correlation begins again.  </p>
  
  <pre><code>  name  date    x   y   Correl
    ABC   1/1/2013    -3.59   -5.13   NA
    ABC   1/2/2013    -8.69   4.22    NA
    ABC   1/3/2013    2.80    -0.59   NA
    ABC   1/4/2013    0.54    5.06    NA
    ABC   1/5/2013    1.13    3.49    -0.03
    ABC   1/6/2013    0.52    5.16    -0.38
    ABC   1/7/2013    -0.24   -5.40   0.08
    ABC   1/8/2013    3.26    -2.75   -0.16
    ABC   1/9/2013    1.33    5.94    -0.04
    ABC   1/10/2013   2.24    1.14    -0.01
    ABC   1/11/2013   0.01    9.87    -0.24
    ABC   1/12/2013   2.29    1.28    -0.99
    ABC   1/13/2013   1.03    -6.30   -0.41
    ABC   1/14/2013   0.62    4.82    -0.47
    ABC   1/15/2013   1.08    -1.17   -0.50
    ABC   1/16/2013   2.43    8.86    0.45
    ABC   1/17/2013   -3.43   9.38    -0.35
    ABC   1/18/2013   -5.73   7.59    -0.38
    ABC   1/19/2013   1.77    3.13    -0.44
    ABC   1/20/2013   -0.97   -0.77   -0.24
    XYZ   1/1/2013    2.12    10.22   NA
    XYZ   1/2/2013    -0.81   0.22    NA
    XYZ   1/3/2013    -1.55   -2.25   NA
    XYZ   1/4/2013    -4.53   3.63    NA
    XYZ   1/5/2013    2.95    -1.51   0.13
    XYZ   1/6/2013    6.76    24.16   0.69
    XYZ   1/7/2013    3.33    7.31    0.66
    XYZ   1/8/2013    -1.47   -4.23   0.67
    XYZ   1/9/2013    3.89    -0.43   0.81
    XYZ   1/10/2013   5.63    17.95   0.86
    XYZ   1/11/2013   3.29    -7.09   0.63
    XYZ   1/12/2013   6.03    -9.03   0.29
    XYZ   1/13/2013   -5.63   6.96    -0.19
    XYZ   1/14/2013   1.70    13.59   -0.18
    XYZ   1/15/2013   -1.19   -16.79  -0.29
    XYZ   1/16/2013   4.76    4.91    -0.11
    XYZ   1/17/2013   9.02    25.16   0.57
    XYZ   1/18/2013   4.56    6.48    0.84
    XYZ   1/19/2013   5.30    11.81   0.99
    XYZ   1/20/2013   -0.60   3.38    0.84
  </code></pre>
  
  <p>UPDATE: I have tried all of your suggestions and have run into problems using the actual data.  I have attached a subset of the data below:</p>
  
  <p><a href="https://www.dropbox.com/s/6k4xhwuinlu0p1f/TEST_SUBSET.csv?dl=0" rel="nofollow">https://www.dropbox.com/s/6k4xhwuinlu0p1f/TEST_SUBSET.csv?dl=0</a></p>
  
  <p>I cannot get this to work.  I've tried removing the NA's, renaming the rows, reading the data in differently, formatting the date differently.  Nothing is working for me.  Can you see if what you are running is working for this dataset?  Thank you very much folks!</p>