Consider the following example
library(tidyverse)
library(lubridate)
time <- seq(from =ymd(\"2014-02-24\"),to= ymd(\"2014-03-20\"), by=\"days\")
set.seed(
aggregate(df2$values,by=list(week(df2$time)),mean)
Group.1 x 1 8 30.00000 2 9 40.00000 3 10 36.42857 4 11 37.85714 5 12 43.33333
This uses the week function of lubridate and gives the week number of the week in the year.
To control which day of the week is the starting day just refer to this thread on that topic:
Changing lubridate function to start on Monday rather than Sunday
The solution from that thread by nograpes suggests that if you want a custom version of the week() function using an arbitrary day of the week as the beginning of the week that you just construct it from base R like this:
start.of.week <- function(date) date - (setNames(c(6,0:5),0:6) [strftime(date,'%w')]) end.of.week <- function(date) date + (setNames(c(0,6:1),0:6) [strftime(date,'%w')]) start.of.week(as.Date(c('2014-01-05','2014-10-02','2014-09-22','2014-09-27'))) # "2013-12-30" "2014-09-29" "2014-09-22" "2014-09-22" end.of.week(as.Date(c('2014-01-05','2014-10-02','2014-09-22','2014-09-27'))) # "2014-01-05" "2014-10-05" "2014-09-28" "2014-09-28"
In the future lubridate will have this option for an arbitrary start day for weeks, but Hadley hasn't got around to adding it yet (https://github.com/hadley/lubridate/issues/257).