R:如何判断Date在同一周? [英] R: How to judge Date in the same week?

查看:16
本文介绍了R:如何判断Date在同一周?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的列来表示同一周内的日期.

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-WwwISO 8601 周格式.它包括根据OP的要求区分不同年份中不同周所需的年份.

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 属于最后一个ISO 2015 年第 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).

按照建议这里, cut.Date() 可能是 OP 的最佳选择.

As suggested here, cut.Date() might be the best option for the 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:如何判断Date在同一周?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆