For some applications, you may want to keep the week represented by a date (usually the start day of the week) instead of a number, here an example using a specialized package (tsibble).
library(dplyr)
library(tsibble)
library(lubridate)
# Sample data on a copy/paste friendly format
mydf <- data.frame(
date = seq.Date(as.Date("1990-01-01"), as.Date("1990-01-31"), by = 1),
val = 0.01
)
# Relevant code
mydf %>%
as_tsibble(index = date) %>%
index_by(week = ~ floor_date(., unit = "week", week_start = 1)) %>% # weekly aggregates
summarise(
sumval = sum(val, na.rm = TRUE)
)
#> # A tsibble: 5 x 2 [7D]
#> week sumval
#> <date> <dbl>
#> 1 1990-01-01 0.07
#> 2 1990-01-08 0.07
#> 3 1990-01-15 0.07
#> 4 1990-01-22 0.07
#> 5 1990-01-29 0.03