You may use date2ISOweek()
from the ISOweek
package. Using stringr
for pattern updating and dplyr
to produce the week-results; The code would be like this, after loading the packages.
library(ISOweek)
library(stringr)
library(dplyr)
# mocking up some data
OBX <- data.frame(
Date = c("2010-12-28", "2010-12-29", "2010-12-30", "2010-12-31", "2011-01-03", "2011-01-04"),
OBX = 101:106
)
# transform form month- to week-based and trim some
OBX$dateWeek <- date2ISOweek(OBX$Date)
# get rid of the preceeding "W", next drop day-of-week
OBX$dateWeek <- stringr::str_replace(OBX$dateWeek, "-W", "-")
OBX$dateWeek <- stringr::str_replace(OBX$dateWeek, "-.$", "")
# sum per week and print
OBX %>%
group_by(dateWeek) %>%
summarise(weekSum = sum(OBX))
# the results of the mocked data
# A tibble: 2 × 2
dateWeek weekSum
<chr> <int>
1 2010-52 410
2 2011-01 211
Please let me know whether this is an answer fitting your needs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…