简体   繁体   English

从周的年份和年份获得周开始日期

[英]Get week starting date from weeknum and year in R

I'm looking for a function that will get year + week number + Week day, and return a date, for example: 我正在寻找能够获得年+周数+周日的功能,并返回日期,例如:

I would like to input the 3 following 我想输入以下3个

 2015
 Monday
 23

And get the desired output: 并获得所需的输出:

"2015-06-08"

After Searching in the Web, there seems to be equivalent question in other languages but not in R: 在网上搜索之后,似乎在其他语言中存在相同的问题但在R中没有:

How to Get date from week number and year 如何从周数和年份获取日期

Any help on that would be great! 任何帮助都会很棒!

Using strptime : 使用strptime

strptime("2015Monday23", "%Y%A%U")
# [1] "2015-06-08"

Or more generally 或者更一般地说

strptime(paste0(2015, "Monday", 23), "%Y%A%U")

There are two caveats here: 这里有两个警告:

  1. The result depends on the current locale. 结果取决于当前的区域设置。
    In my locale "German_Germany.1252" (call Sys.getlocale("LC_TIME") to check your locale), strptime("2015Monday23", "%Y%A%U") returns NA . 在我的语言环境"German_Germany.1252" (调用Sys.getlocale("LC_TIME")来检查您的语言环境), strptime("2015Monday23", "%Y%A%U")返回NA

  2. The results depends on the convention for numbering the weeks of a year. 结果取决于一年中数周的编号惯例。
    There are 3 conventions R is aware of: US, UK, and ISO 8601. See this answer for a detailed discussion. R有三个约定:US,UK和ISO 8601.有关详细讨论,请参阅此答案 So, the convention to be used for conversion has to be specified. 因此,必须指定用于转换的约定。

Non-English locales 非英语语言环境

If you are in a non-english locale, you can deal with English weekday names (or month names, likewise) by temporarily changing the current locale: 如果您使用的是非英语语言环境,则可以通过临时更改当前语言环境来处理英语工作日名称(或同样的月份名称):

Sys.setlocale("LC_TIME", "US")
#> [1] "English_United States.1252"
strptime("2015Monday23", "%Y%A%U")
#> [1] "2015-06-08 CEST"
Sys.setlocale("LC_TIME")
#> [1] "German_Germany.1252"

The lubridate package offers a more convenient way: lubridate包提供了一种更方便的方式:

lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"

Week of the year in different conventions 一年中的各个不同的公约

As the weekday is given by its name, the US and UK conventions return the same result: 由于工作日的名称,美国和英国的公约返回相同的结果:

lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"
lubridate::parse_date_time("2015Monday23", "YAW", locale = "UK")
#> [1] "2015-06-08 UTC"

Unfortunately, the format specifiers for the ISO 8601 convention are not accepted on input. 遗憾的是,输入时不接受ISO 8601约定的格式说明符。 So, we reverse the process and format the resulting date as week of the year in the different conventions which shows a differing result for ISO 8601. 因此,我们颠倒过程并将结果日期格式化为不同约定中的一年中的一周,这显示了ISO 8601的不同结果。

format(as.Date("2015-06-08 UTC"), "%Y-%W-%u") # UK convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%Y-%U-%w") # US convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%G-%V-%u") # ISO 8601
#> [1] "2015-24-1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM