title: Convert Daily Data into Weekly in R Week Starts on Saturday
body: <p>I am having trouble converting daily data into weekly using averages over the week.</p>
  
  <p>My Data looks like this:</p>
  
  <pre><code>&gt; str(daily_FWIH)
  'data.frame':   4371 obs. of  6 variables:
   $ Date     : Date, format: "2013-03-01" "2013-03-02" "2013-03-04" "2013-03-05" ...
   $ CST.OUC  : Factor w/ 6 levels "BVG11","BVG12",..: 1 1 1 1 1 1 1 1 1 1 ...
   $ CST.NAME : Factor w/ 6 levels "Central Scotland",..: 2 2 2 2 2 2 2 2 2 2 ...
   $ SOM_patch: Factor w/ 6 levels "BVG11_Highlands &amp; Islands",..: 1 1 1 1 1 1 1 1 1 1 ...
   $ Row_Desc : Factor w/ 1 level "FSFluidWIH": 1 1 1 1 1 1 1 1 1 1 ...
   $ Value    : num  1.16 1.99 1.47 1.15 1.16 1.28 1.27 2.07 1.26 1.19 ...
  
  &gt; head(daily_FWIH)
          Date CST.OUC            CST.NAME                 SOM_patch   Row_Desc Value
  1 2013-03-01   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.16
  2 2013-03-02   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.99
  3 2013-03-04   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.47
  4 2013-03-05   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.15
  5 2013-03-06   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.16
  6 2013-03-07   BVG11 Highlands &amp; Islands BVG11_Highlands &amp; Islands FSFluidWIH  1.28
  </code></pre>
  
  <p>This is what I've tried converting this to xts object as shown <a href="https://stackoverflow.com/questions/11892063/how-to-convert-a-daily-times-series-into-an-averaged-weekly">here</a></p>
  
  <p>This is what I have tried:</p>
  
  <pre><code>daily_FWIH$Date = as.Date(as.character(daily_FWIH$Date), "%d/%m/%Y")
  library(xts)
  
  temp.x = xts(daily_FWIH[-1], order.by=daily_FWIH$Date)
  apply.weekly(temp.x, colMeans(temp.x$Value))
  </code></pre>
  
  <p>I have two problems. My week starts and ends on a <strong>"Saturday"</strong> and I get the following error:</p>
  
  <pre><code>&gt; apply.weekly(temp.x, colMeans(temp.x$Value))
  Error in colMeans(temp.x$Value) : 'x' must be numeric
  </code></pre>
  
  <p><strong>UPDATE</strong> Based on Sam's comments:</p>
  
  <p>This is what I did:</p>
  
  <pre><code>daily_FWIH$Date &lt;- ymd(daily_FWIH$Date) # convert to POSIX format
  daily_FWIH$fakeDate &lt;- daily_FWIH$Date + days(2)
  daily_FWIH$week &lt;- week(daily_FWIH$fakeDate) # extract week value
  daily_FWIH$year &lt;- year(daily_FWIH$fakeDate)
  
      &gt; daily_FWIH %&gt;%
  + group_by(year,week) %&gt;%
  + mutate(weeklyAvg = mean(Value), weekStartsOn = min(Date)) %&gt;% # create the average variable
  + slice(which(Date == weekStartsOn)) %&gt;% # select just the first record of the week - other vars will come from this
  + select(-Value,-fakeDate,-week,-year,-Date, -CST.OUC,-CST.NAME) # drop unneeded variables
  Source: local data frame [631 x 6]
  Groups: year, week
  
     year week                   SOM_patch   Row_Desc weeklyAvg weekStartsOn
  1  2013    9   BVG11_Highlands &amp; Islands FSFluidWIH  1.048333   2013-03-01
  2  2013    9   BVG12_North East Scotland FSFluidWIH  1.048333   2013-03-01
  3  2013    9      BVG13_Central Scotland FSFluidWIH  1.048333   2013-03-01
  4  2013    9   BVG14_South East Scotland FSFluidWIH  1.048333   2013-03-01
  5  2013    9 BVG15_West Central Scotland FSFluidWIH  1.048333   2013-03-01
  6  2013    9   BVG16_South West Scotland FSFluidWIH  1.048333   2013-03-01
  7  2013   10   BVG11_Highlands &amp; Islands FSFluidWIH  1.520500   2013-03-02
  8  2013   10   BVG12_North East Scotland FSFluidWIH  1.520500   2013-03-02
  9  2013   10      BVG13_Central Scotland FSFluidWIH  1.520500   2013-03-02
  10 2013   10   BVG14_South East Scotland FSFluidWIH  1.520500   2013-03-02
  ..  ...  ...                         ...        ...       ...          ...
  </code></pre>
  
  <p>Which is incorrect...</p>
  
  <p>The desired output is:</p>
  
  <pre><code>&gt; head(desired)
          Date BVG11.Highlands_I_.A_pct BVG12.North.East.ScotlandA_pct BVG13.Central.ScotlandA_pct
  1 01/03/2013                     1.16                           1.13                        1.08
  2 08/03/2013                     1.41                           2.37                        1.80
  3 15/03/2013                     1.33                           3.31                        1.34
  4 22/03/2013                     1.39                           2.49                        1.62
  5 29/03/2013                     5.06                           3.42                        1.42
  6                                  NA                             NA                          NA
    BVG14.South.East.ScotlandA_pct BVG15.West.Central.ScotlandA_pct BVG16.South.West.ScotlandA_pct
  1                           1.05                             0.98                           0.89
  2                           1.51                             1.21                           1.07
  3                           1.13                             2.13                           2.01
  4                           2.14                             1.24                           1.37
  5                           1.62                             1.46                           1.95
  6                             NA                               NA                             NA
  
  &gt; str(desired)
  'data.frame':   11 obs. of  7 variables:
   $ Date                            : Factor w/ 6 levels "01/03/2013",..: 2 3 4 5 6 1 1 1 1 1 ...
   $ BVG11.Highlands_I_.A_pct        : num  1.16 1.41 1.33 1.39 5.06  ...
   $ BVG12.North.East.ScotlandA_pct  : num  1.13 2.37 3.31 2.49 3.42  ...
   $ BVG13.Central.ScotlandA_pct     : num  1.08 1.8 1.34 1.62 1.42  ...
   $ BVG14.South.East.ScotlandA_pct  : num  1.05 1.51 1.13 2.14 1.62  ...
   $ BVG15.West.Central.ScotlandA_pct: num  0.98 1.21 2.13 1.24 1.46 ...
   $ BVG16.South.West.ScotlandA_pct  : num  0.89 1.07 2.01 1.37 1.95 ...
  </code></pre>