Из документации strptime :
%U
Week of the year as decimal number (00–53) using Sunday as the first day 1 of the
week (and typically with the first Sunday of the year as day 1 of week 1). The US
convention.
%V
Week of the year as decimal number (01–53) as defined in ISO 8601. If the week
(starting on Monday) containing 1 January has four or more days in the new year,
then it is considered week 1. Otherwise, it is the last week of the previous year,
and the next week is week 1. (Accepted but ignored on input.)
%W
Week of the year as decimal number (00–53) using Monday as the first day of week
(and typically with the first Monday of the year as day 1 of week 1). The UK
convention.
Звучит так, что вам может потребоваться либо %U
, либо %W
, в зависимости от того, хотите ли вы рассматривать воскресенье или понедельник как начало недели.
Обратите внимание, что это может привести к значениям от 00
до 53
, что является следствием привязки начала недели к определенному дню недели (воскресенье или понедельник). Это означает, что на самом деле может быть неполная неделя в начале и в конце года.
Если вы предпочитаете считать на основе недели номер 1, начинающейся в первый день года, вы можете использовать функция lubridate::week
.
Например:
library(lubridate)
year_week <- function(date) paste0(year(date), ' W', week(date))
year_week(ymd("2019-01-01"))
# Result: "2019 W1"
year_week(ymd("2019-12-30"))
# Result: "2019 W52"