【问题标题】:Get all the weeks in the period with R使用 R 获取该期间的所有周数
【发布时间】:2020-11-02 08:48:31
【问题描述】:

我将编写代码,因为它很难解释。我想做以下事情。

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

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

【问题讨论】:

  • 你能发布预期的输出吗?
  • Can you make the above code smarter? 我认为预期的输出是 df 但 OP 想要其他/更好的方法。

标签: r lubridate


【解决方案1】:

您可以创建一系列每周日期,从每一天中减去当前工作日得到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

【讨论】:

  • 谢谢你说问题不重复。我正在寻找的大部分代码。但是有些日期有错误。 y &lt;- as.Date("2020-04-30") y &lt;- as.Date("2020-07-31") 如果您将这些日期设置为 end_date,它们将被检索到下周。你知道原因吗?
  • @TimIkata 也许,您可以使用subset 删除此类日期。更新了答案。
【解决方案2】:

您可以采用这样的方法。它按 ISO 周数分组,并从每个组的示例数据中给出最小/最大日期。
您可以更改by-参数,以获取您需要的组。例如lubridate::epiweek(),如果您需要几周才能从星期日开始,请参阅底部。

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

每周从星期一开始

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

【讨论】:

  • 感谢您的回答。这是一个比我更聪明的代码。但是,当周数为 1 时,开始是 2019-12-29,当周数为 14 时,我想得到 2020-04-04。这可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多