通常,我会通过标准日期格式掩码来计算一年中的第几周%Y%W
。 到 2019 年为止,它运行良好,但是最近几天我注意到 2019 年的第一周 (2018/12/31-2019/01/06) 显示为 2018 年的最后一周第 53 周。 只有 2019/01/07-2019/01/13 这一周是 2019 年的第一周。 我已经检查过它是否正确。
到处都可以看到,2018/12/31-2019/01/06 这周目前是 2019 年的第一周。甚至 Oracle 也知道这一点to_char(..., 'yyyyiw')
,但 R 不知道。至少它应该有相同的观点。
这对我来说确实很重要,我认为它看起来像是一个错误。我正在使用 R 3.5.0。
答案1
看?strftime
:
‘%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.
Oracle 中的格式掩码 IW 返回ISO 周数年度,即%V
R 中:
R> x <- as.Date("2018-12-28") + 0:10
R> data.frame(date=x, weeknum=format(x, "%V"))
date weeknum
1 2018-12-28 52
2 2018-12-29 52
3 2018-12-30 52
4 2018-12-31 01
5 2019-01-01 01
6 2019-01-02 01
7 2019-01-03 01
8 2019-01-04 01
9 2019-01-05 01
10 2019-01-06 01
11 2019-01-07 02