转换年/周至今的对象 [英] Transform year/week to date object

查看:122
本文介绍了转换年/周至今的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串包含'YEAR WEEK',我想用parse_date_time()将其转换为日期对象,但是我无法使代码正常工作

String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work:

parse_date_time(c("201510"), "YW")

我不必使用 lubridate ,也可以使用其他软件包.

I don't have to use lubridate, can be other packages, too.

推荐答案

在将年-周转换为日期之前,您必须指定一周中的某一天,但更重要的是,您必须确保哪些是不同的约定正在使用.

Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used.

Base R的strptime()函数知道一年中一周的3种定义(但在输入中仅支持其中2种)和工作日数字的2种定义, 参见?strptime:

Base R's strptime() function knows 3 definitions for week of the year (but supports only 2 of them on input) and 2 definitions for weekday numbers, see ?strptime:

  • 美国惯例:一年中的星期作为十进制数字(00-53),使用星期日作为一周的第一天(通常以一年中的第一个星期日作为星期几)第1周的1):%U

  • US convention: 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): %U

英国约定:一年中的星期作为十进制数字(00-53),以星期一作为一周的第一天(通常以该年的第一个星期一作为日历的第1天)第1周):%W

UK convention: 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): %W

ISO 8601定义:一年中的星期,为ISO 8601中定义的十进制数字(01-53).如果包含1月1日的那一周(从星期一开始)有四个或更多新年中的天数,则将其视为第1周.否则,它是上一年的最后一周,而下周是第1周:%V,该输入被接受但在输入时被忽略.
请注意,还有一个基于周的年份(%G%g)将与%V一起使用,因为它可能不同于日历年(%Y%y).

ISO 8601 definition: 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: %V which is accepted but ignored on input.
Note that there is also a week-based year (%G and %g) which is to be used with %V as it may differ from the calendar year (%Y and %y).

  • 工作日为十进制数字(1–7,星期一为1):%u
  • 工作日为十进制数字(0–6,星期日为0):%w
  • 有趣的是,对于星期日被视为一周的第一天的情况,没有格式.
  • Weekday as a decimal number (1–7, Monday is 1): %u
  • Weekday as decimal number (0–6, Sunday is 0): %w
  • Interestingly, there is no format for the case Sunday is counted as day 1 of the week.

如果我们将第1天附加到字符串中,并使用不同的格式,则会得到

If we append day 1 to the string and use the different formats we do get

as.Date("2015101", "%Y%U%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%U%w")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%w")
# [1] "2015-03-09"
as.Date("2015101", "%G%V%u")
# [1] NA

对于工作日格式%u%w,我们确实得到了相同的结果,因为两种约定的第一天都是星期一(但是在处理星期日时要小心).

For weekday formats %u and %w we do get the same result because day 1 is Monday in both conventions (but watch out when dealing with Sundays).

对于2015年,美国和英国在一年中一周的定义是重合的,但并非对所有年份都是如此,例如,对于2001、2007和2018年而言并非如此:

For 2015, the US and the UK definition for week of the year coincide but this is not true for all years, e.g., not for 2001, 2007, and 2018:

as.Date("2018101", "%Y%U%u")
#[1] "2018-03-12"
as.Date("2018101", "%Y%W%u")
#[1] "2018-03-05"

输入不支持ISO 8601格式说明符.因此,几年前,我创建了 ISOweek软件包 :

The ISO 8601 format specifiers aren't supported on input. Therefore, I had created the ISOweek package some years ago:

ISOweek::ISOweek2date("2015-W10-1")
#[1] "2015-03-02"

修改:使用星期四将一周与一个月相关联

如上所述,您需要指定一周中的某一天才能获得完整的日历日期.如果需要在一个月后汇总日期,则也需要这样做.

Using Thursday to associate a week with a month

As mentioned above you need to specify a day of the week to get a full calendar date. This is also required if the dates need to be aggregated by month later on.

如果未指定工作日,并且应该按月汇总日期,则可以将每周的星期四作为参考日(以下

If no weekday is specified and if the dates are supposed to be aggregated by month later on, you may take the Thursday of each week as reference day (following a suggestion by djhurio). This ensures that the whole week is assigned to the month to which the majority of the days of the week belong to.

例如,以星期日为参考日将返回

For example, taking Sunday as reference day would return

ISOweek::ISOweek2date("2015-W09-7")

[1] "2015-03-01"

因此会将整个星期与3月关联,尽管一周中只有一天属于3月,而其他6天属于2月.以星期四为参考日将返回2月的日期:

which consequently would associate the whole week to the month of March although only one day of the week belongs to March while the other 6 days belong to February. Taking Thursday as reference day will return a date in February:

ISOweek::ISOweek2date("2015-W09-4")

[1] "2015-02-26"

这篇关于转换年/周至今的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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