R:如何判断同一周的日期? [英] R: How to judge Date in the same week?
问题描述
我想创建一个新的枚举来表示同一周的日期。
I want create a new colume to represent which date are in the same week.
data.table DATE_SET包含日期信息,例如:
A data.table DATE_SET contains Date information, like:
DATA_SET<- data.table(transday = seq(from = (Sys.Date()-64), to = Sys.Date(), by = 1))
例如, 2017-03-01和 2017-03-02在同一周, 2017-03-01和 2017- 03-08'都是星期三,但不在同一周。
For example, '2017-03-01' and '2017-03-02' are in the same week, '2017-03-01' and '2017-03-08' both Wednesday, but they are not in the same week.
如果 2016-01-01是2016年的第一周, 2017-01-01是2017年的第一周,则值为1,但是他们不在同一周。因此,我希望唯一的值能表示同一周。
If "2016-01-01" is the first week in 2016, "2017-01-01" is the first week in 2017, the value is 1, but they are not in the same week. So i want the unique value to pecify "a same week".
推荐答案
这个问题的答案很大程度上取决于
The answer to this question depends strongly on
- 一周中第一天(通常是星期日或星期一)的定义和
- 一年中的几周(从一年中的第一个星期日,星期一或星期四或1月1日开始,等等)。
从下面的示例中可以看到不同的选择:
A selection of different options can be seen from the example below:
dates isoweek day week_iso week_US week_UK DT_week DT_iso lub_week lub_iso cut.Date
2015-12-25 2015-W52 5 2015-W52 51 51 52 52 52 52 2015-12-21
2015-12-26 2015-W52 6 2015-W52 51 51 52 52 52 52 2015-12-21
2015-12-27 2015-W52 7 2015-W52 52 51 52 52 52 52 2015-12-21
2015-12-28 2015-W53 1 2015-W53 52 52 52 53 52 53 2015-12-28
2015-12-29 2015-W53 2 2015-W53 52 52 52 53 52 53 2015-12-28
2015-12-30 2015-W53 3 2015-W53 52 52 53 53 52 53 2015-12-28
2015-12-31 2015-W53 4 2015-W53 52 52 53 53 53 53 2015-12-28
2016-01-01 2015-W53 5 2015-W53 00 00 1 53 1 53 2015-12-28
2016-01-02 2015-W53 6 2015-W53 00 00 1 53 1 53 2015-12-28
2016-01-03 2015-W53 7 2015-W53 01 00 1 53 1 53 2015-12-28
2016-01-04 2016-W01 1 2016-W01 01 01 1 1 1 1 2016-01-04
2016-01-05 2016-W01 2 2016-W01 01 01 1 1 1 1 2016-01-04
2016-01-06 2016-W01 3 2016-W01 01 01 1 1 1 1 2016-01-04
2016-01-07 2016-W01 4 2016-W01 01 01 2 1 1 1 2016-01-04
2016-01-08 2016-W01 5 2016-W01 01 01 2 1 2 1 2016-01-04
由以下代码创建:
library(data.table)
dates <- as.Date("2016-01-01") + (-7:7)
print(data.table(
dates,
isoweek = ISOweek::ISOweek(dates),
day = ISOweek::ISOweekday(dates),
week_iso = format(dates, "%G-W%V"),
week_US = format(dates, "%U"),
week_UK = format(dates, "%W"),
DT_week = data.table::week(dates),
DT_iso = data.table::isoweek(dates),
lub_week = lubridate::week(dates),
lub_iso = lubridate::isoweek(dates),
cut.Date = cut.Date(dates, "week")
), row.names = FALSE)
某些列中使用的格式 YYYY-Www
是 ISO 8601周格式。
The format YYYY-Www
used in some of the columns is one of the ISO 8601 week formats. It includes the year which is required to distinguish different weeks in different years as requested by the OP.
ISO星期定义是唯一可确保每周始终包含以下内容的格式:也是跨新年的7天。其他定义可以以少于7天的周开始或结束年份。由于年份的无缝分配, ISO周编号年份与传统的公历年份略有不同,例如, 2016-01-01
属于2015年最后一个ISO周53( 2015-W53
)。
The ISO week definition is the only format which ensures that each week always consists of 7 days, also across New Year. The other definitions may start or end the year with "weeks" with less than 7 days. Due to the seamless partioning of the year, the ISO week-numbering year is slightly different from the traditional Gregorian calendar year, e.g., 2016-01-01
belongs to the last ISO week 53 of 2015 (2015-W53
).
如建议此处,切.date()
可能是OP的最佳选择。
As suggested here, cut.Date()
might be the best option for the OP.
披露:我是OP的维护者 ISOweek
软件包,该软件包在 strptime()
无法识别%G时发布
和%V
格式规范,用于Windows版本的R中的输出。(至今仍无法在输入中识别它们。)
Disclosure: I'm maintainer of the ISOweek
package which was published at a time when strptime()
did not recognize the %G
and %V
format specifications for output in the Windows versions of R. (Still today they aren't recognized on input).
这篇关于R:如何判断同一周的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!