首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ISO 8601年周号与Windows上与德国地区的月份号相匹配

将ISO 8601年周号与Windows上与德国地区的月份号相匹配
EN

Stack Overflow用户
提问于 2017-01-12 15:03:17
回答 3查看 812关注 0票数 2

这与我的问题从每周时间格式的日期开始的日期直接相关。

但是,在这个问题中,我想特别询问如何将ISO 8601周数映射到年中的月份数。

在我看来,这似乎是不可能的和/或涉及一些非直觉的黑客(甚至这些都不能真正可靠地工作)和海事组织因此应该被认为是需要在基础R修复。不过,如果我错了,请纠正我

编辑:似乎这个问题与在Windows上运行和/或您所处的地区密切相关(我的例子是标准德语)。

代码语言:javascript
复制
posix <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01", "2016-01-08"))

ISO 8601

代码语言:javascript
复制
(yw <- format(posix, "%Y-%V"))
# [1] "2015-52" "2015-53" "2016-53" "2016-01"
ywd <- sprintf("%s-1", yw)
(as.POSIXct(ywd, format = "%Y-%V-%u"))
# [1] "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"
# -> utterly wrong!!!

ywd <- sprintf("%s-4", yw)
(as.POSIXct(ywd, format = "%Y-%V-%u"))
# -> still wrong -> the day of the week is not the reason

# -> no way to use ISO 8601 convention to map week of the year to month of the year

为了适当的扩张:在试图使用美国或英国的公约时也是不可能的:

美国公约

代码语言:javascript
复制
(yw <- format(posix, "%Y-%U"))
# [1] "2015-51" "2015-52" "2016-00" "2016-01"
ywd <- sprintf("%s-1", yw)
(as.POSIXct(ywd, format = "%Y-%U-%u"))
# [1] "2015-12-21 CET" "2015-12-28 CET" NA               "2016-01-04 CET"
# -> NA problem for week 00

ywd <- sprintf("%s-4", yw)
# -> does not work for week 00
(as.POSIXct(ywd, format = "%Y-%U-%u"))
# The day of the week is not the reason

# -> no way to use this convention to reliably map week of the year to month of the year

英国公约

代码语言:javascript
复制
(yw <- format(posix, "%Y-%W"))
# [1] "2015-51" "2015-52" "2016-00" "2016-01"
ywd <- sprintf("%s-1", yw)
(as.POSIXct(ywd, format = "%Y-%W-%u"))
# [1] "2015-12-21 CET" "2015-12-28 CET" NA               "2016-01-04 CET"
# -> NA problem for week 00

ywd <- sprintf("%s-4", yw)
# -> does not work for week 00
(as.POSIXct(ywd, format = "%Y-%W-%u"))
# The day of the week is not the reason

# -> no way to use this convention to reliably map week of the year to month of the year

会话信息

代码语言:javascript
复制
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=German_Germany.1252     LC_CTYPE=German_Germany.1252       LC_MONETARY=German_Germany.1252   
[4] LC_NUMERIC=C                       LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] fva_0.1.0       digest_0.6.10   readxl_0.1.1    dplyr_0.5.0     plyr_1.8.4      magrittr_1.5   
 [7] memoise_1.0.0   testthat_1.0.2  roxygen2_5.0.1  devtools_1.12.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8     lubridate_1.6.0 assertthat_0.1  packrat_0.4.8-1 crayon_1.3.2    withr_1.0.2    
 [7] R6_2.2.0        DBI_0.5-1       stringi_1.1.2   rstudioapi_0.6  tools_3.3.2     stringr_1.1.0  
[13] tibble_1.2     

> devtools::session_info()
Session info -----------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.2 (2016-10-31)
 system   x86_64, mingw32             
 ui       RStudio (1.0.136)           
 language en                          
 collate  German_Germany.1252         
 tz       Europe/Berlin               
 date     2017-01-12                  

Packages ---------------------------------------------------------------------------------------------------
 package    * version date       source        
 assertthat   0.1     2013-12-06 CRAN (R 3.3.2)
 crayon       1.3.2   2016-06-28 CRAN (R 3.3.2)
 DBI          0.5-1   2016-09-10 CRAN (R 3.3.2)
 devtools   * 1.12.0  2016-06-24 CRAN (R 3.3.2)
 digest     * 0.6.10  2016-08-02 CRAN (R 3.3.2)
 dplyr      * 0.5.0   2016-06-24 CRAN (R 3.3.2)
 fva        * 0.1.0   <NA>       local         
 lubridate    1.6.0   2016-09-13 CRAN (R 3.3.2)
 magrittr   * 1.5     2014-11-22 CRAN (R 3.3.2)
 memoise    * 1.0.0   2016-01-29 CRAN (R 3.3.2)
 packrat      0.4.8-1 2016-09-07 CRAN (R 3.3.2)
 plyr       * 1.8.4   2016-06-08 CRAN (R 3.3.2)
 R6           2.2.0   2016-10-05 CRAN (R 3.3.2)
 Rcpp         0.12.8  2016-11-17 CRAN (R 3.3.2)
 readxl     * 0.1.1   2016-03-28 CRAN (R 3.3.2)
 roxygen2   * 5.0.1   2015-11-11 CRAN (R 3.3.2)
 stringi      1.1.2   2016-10-01 CRAN (R 3.3.2)
 stringr      1.1.0   2016-08-19 CRAN (R 3.3.2)
 testthat   * 1.0.2   2016-04-23 CRAN (R 3.3.2)
 tibble       1.2     2016-08-26 CRAN (R 3.3.2)
 withr        1.0.2   2016-06-20 CRAN (R 3.3.2)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-07 09:34:02

信息披露:,正如这个答案中提到的,我创建了套餐来处理基于ISO 8601周的日期。

这个问题有几个缺陷:

  1. 基于ISO 8601周的年份与日历年不同.
  2. 如果不指定一周中的某一天,那么每年一周到一年一个月的转换是不明确的。

以周为基础的年份与历年

OP使用以下方法创建了示例数据

代码语言:javascript
复制
posix <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01", "2016-01-08"))
(yw <- format(posix, "%Y-%V"))

1 "2015-52“"2015-53”"2016-53“"2016-01”

格式规范%Y返回日历年,这显然对第三个元素是错误的。

有了正确的格式规范%G,我们就可以得到

代码语言:javascript
复制
(yw <- format(posix, "%G-%V"))

1 "2015-52“"2015-53”"2015-53“"2016-01”

年周至年月的换算

仅仅提供基于ISO周的年份和周号,而不使用,就会产生不明确的结果。

这可以用(校正的)样本数据来演示,这些数据现在包含了OP自己的(非标准的)年度周格式的连续三个星期:

代码语言:javascript
复制
yw

1 "2015-52“"2015-53”"2016-01“

借助来自ISOweek2date()包的ISOweek函数,数据被转换为日历日期。请注意,ISOweek2date()需要一个完整的基于yyyy-Www-d格式的基于8601周的日期,包括星期的日期。如果我们选择一周的第一天(星期一),我们会得到:

代码语言:javascript
复制
library(ISOweek)
library(magrittr)
yw %>% 
  # insert "W" to conform with ISO 8601 format
  sub("-", "-W", .) %>% 
  # append day of week
  paste0("-1") %>%
  # convert to class Date and print as yyyy-mm 
  ISOweek2date() %>% 
  format("%Y-%m")

1 "2015-12“"2015-12”"2016-01“

现在,我们使用一周的最后一天(星期日)重复如下:

代码语言:javascript
复制
yw %>% 
  sub("-", "-W", .) %>% 
  paste0("-7") %>% 
  ISOweek2date() %>% 
  format("%Y-%m")

1 "2015-12“"2016-01”"2016-01“

请注意,第二个元素现在指的是2016年1月而不是2015年12月,因为第53周的星期日是在1月,而这周的星期一仍然是在12月。

票数 2
EN

Stack Overflow用户

发布于 2017-01-12 19:34:46

parameters格式参数?strptime的文档在输入时表示"%V“将被忽略。

票数 1
EN

Stack Overflow用户

发布于 2017-01-12 15:39:07

非常肯定的是,除了基本的R之外,还需要更改其他东西(参见末尾的注意事项):

代码语言:javascript
复制
some_dates <- as.POSIXct(c("2015-12-24", "2015-12-31", "2016-01-01", "2016-01-08"))

(year_week <- format(some_dates, "%Y %U"))
## [1] "2015 51" "2015 52" "2016 00" "2016 01"

(year_week_day <- sprintf("%s 1", year_week))
## [1] "2015 51 1" "2015 52 1" "2016 00 1" "2016 01 1"

(as.POSIXct(year_week_day, format = "%Y %U %u"))
## [1] "2015-12-21 EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"

它也适用于破折号:

代码语言:javascript
复制
(year_week <- format(some_dates, "%Y-%U"))
## [1] "2015-51" "2015-52" "2016-00" "2016-01"

(year_week_day <- sprintf("%s-1", year_week))
## [1] "2015-51-1" "2015-52-1" "2016-00-1" "2016-01-1"

(as.POSIXct(year_week_day, format = "%Y-%U-%u"))
## [1] "2015-12-21 EST" "2015-12-28 EST" "2016-01-04 EST" "2016-01-04 EST"

而且,尽管虚线是OK ISO表单,但当各种值不超过12或<0时,它们会导致读取器混淆。

注意事项

正如注释线程所指出的,这是Windows上的行为:

代码语言:javascript
复制
(year_week <- format(some_dates, "%Y-%U"))
## [1] "2015-51" "2015-52" "2016-00" "2016-01"

(year_week_day <- sprintf("%s-1", year_week))
## [1] "2015-51-1" "2015-52-1" "2016-00-1" "2016-01-1"

(as.POSIXct(year_week_day, format = "%Y-%U-%u"))
## [1] "2015-12-21 PST" "2015-12-28 PST" NA               "2016-01-04 PST"

(Windows 10 64 this,R3.3.2 for me/本例)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41616407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档