使用 R 获取期间内的所有周数

Get all the weeks in the period with R

我会写代码,因为它很难解释。我想执行以下操作。

x <- as.Date("2020-01-01")
y <- as.Date("2020-03-31") 

library(lubridate)

date <- seq(x, y, by = "day")

start <- floor_date(date, 'weeks')
end <- ceiling_date(date, 'weeks') - 1

df <- data.frame(
  start_date = start,
  end_date = end
)

df <- unique(df)

df
   start_date   end_date
1  2019-12-29 2020-01-04
5  2020-01-05 2020-01-11
12 2020-01-12 2020-01-18
19 2020-01-19 2020-01-25
26 2020-01-26 2020-02-01
33 2020-02-02 2020-02-08
40 2020-02-09 2020-02-15
47 2020-02-16 2020-02-22
54 2020-02-23 2020-02-29
61 2020-03-01 2020-03-07
68 2020-03-08 2020-03-14
75 2020-03-15 2020-03-21
82 2020-03-22 2020-03-28
89 2020-03-29 2020-04-04

我想通过输入句点来输出所有所属的星期。 你能让上面的代码更聪明吗?

您可以采用这样的方法。它按 ISO 周数分组,并根据每个组的样本数据给出 min/max 日期。
您可以更改 by 参数,以获得您需要的组。例如 lubridate::epiweek() 如果您需要周从星期日开始,请参阅底部。

  1. 首先,您必须计算第一周的开始日期和最后一周的结束日期(我假设周从星期一开始,星期日见答案底部)。
  2. 创建一个日期序列,从开始日期到结束日期
  3. 按周数分组,select 每组的最小和最大日期

周从星期一开始

library( data.table )
library( lubridate )

x <- as.Date("2020-01-01")
y <- as.Date("2020-03-31") 

#assuming weeks start on a Monday
# calculate first date of starting week
start = x - lubridate::wday( x, week_start = 1) + 1
# calculate last date of ending week
end   = y + 7 - lubridate::wday( y, week_start = 1 ) 

mydata <- data.table( 
  date = seq( start, end, by = "1 days" )
)
#group dates by ISO weeknumber, return min and max data
mydata[, 
       .( start = min(date), end = max(date) ), 
       by = .( weeknumber = lubridate::isoweek( date ) ) ]

#     weeknumber      start        end
#  1:          1 2019-12-30 2020-01-05
#  2:          2 2020-01-06 2020-01-12
#  3:          3 2020-01-13 2020-01-19
#  4:          4 2020-01-20 2020-01-26
#  5:          5 2020-01-27 2020-02-02
#  6:          6 2020-02-03 2020-02-09
#  7:          7 2020-02-10 2020-02-16
#  8:          8 2020-02-17 2020-02-23
#  9:          9 2020-02-24 2020-03-01
# 10:         10 2020-03-02 2020-03-08
# 11:         11 2020-03-09 2020-03-15
# 12:         12 2020-03-16 2020-03-22
# 13:         13 2020-03-23 2020-03-29
# 14:         14 2020-03-30 2020-04-05

星期日开始一周

start = x - lubridate::wday( x, week_start = 7) + 1  #!! <-- week_start = 7
# calculate last date of ending week
end   = y + 7 - lubridate::wday( y, week_start = 7 ) #!! <-- week_start = 7

mydata <- data.table( 
  date = seq( start, end, by = "1 days" )
)
#group dates by ISO weeknumber, return min and max data
mydata[, 
       .( start = min(date), end = max(date) ), 
       by = .( weeknumber = lubridate::epiweek( date ) ) ] #!! <-- epiweek

#     weeknumber      start        end
#  1:          1 2019-12-29 2020-01-04
#  2:          2 2020-01-05 2020-01-11
#  3:          3 2020-01-12 2020-01-18
#  4:          4 2020-01-19 2020-01-25
#  5:          5 2020-01-26 2020-02-01
#  6:          6 2020-02-02 2020-02-08
#  7:          7 2020-02-09 2020-02-15
#  8:          8 2020-02-16 2020-02-22
#  9:          9 2020-02-23 2020-02-29
# 10:         10 2020-03-01 2020-03-07
# 11:         11 2020-03-08 2020-03-14
# 12:         12 2020-03-15 2020-03-21
# 13:         13 2020-03-22 2020-03-28
# 14:         14 2020-03-29 2020-04-04

您可以创建一个周日期序列,从每一天中减去当前工作日得到 start_date,然后在 start_date 上加上 6 天得到 end_date

create_weekly_dataframe <- function(x, y) {
   seq_dates <- seq(x, y + 7, by = "week")
   start_date <- seq_dates - as.integer(format(x, '%u'))
   subset(data.frame(start_date = start_date, end_date = start_date + 6), 
          start_date <= y)
}

create_weekly_dataframe(x, y)


#   start_date   end_date
#1  2019-12-29 2020-01-04
#2  2020-01-05 2020-01-11
#3  2020-01-12 2020-01-18
#4  2020-01-19 2020-01-25
#5  2020-01-26 2020-02-01
#6  2020-02-02 2020-02-08
#7  2020-02-09 2020-02-15
#8  2020-02-16 2020-02-22
#9  2020-02-23 2020-02-29
#10 2020-03-01 2020-03-07
#11 2020-03-08 2020-03-14
#12 2020-03-15 2020-03-21
#13 2020-03-22 2020-03-28
#14 2020-03-29 2020-04-04