[R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale

David Winsemius dwinsemius at comcast.net
Fri Jan 13 22:33:17 CET 2017


> On Jan 13, 2017, at 3:20 AM, Janko Thyson <janko.thyson at gmail.com> wrote:
> 
> Hi David,
> 
> thanks for replying and sorry about the HTML/non-plain-text email (I
> forgot to change that, shouldn't have happened).
> 
> Might just be me, but reading "The documentation for R datetime format
> parameters ?strptime says %V is ignored on input." in the
> documentation doesn't really tell me all that much. As a user, I would
> read that, not completely understand what this means and thus try to
> understand it better by applying it in actual code:
> 
> (yw <- format(posix, "%Y-%V"))
>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
> 
> Which, after checking back with a calendar, would give me reason to
> believe that it using %V does in fact seem to work: it's an input to
> `format()` and R doesn't seem to ignore it as the correct week numbers
> (following ISO 8601) are returned.
> 
> Not wanting to stress this particular aspect any further, though, I
> would slightly rephrase my original question: is it possible to use
> the ISO 8601 convention for weeknumbers at all (on Windows, using a
> German locale setting) and if so, how would I link ISO 8601
> weeknumbers to the correct month of the year?

You are using an undocumented behavior of R's implementation on Windows. If you have a date, it's unambiguous what month and dates should be returned, but I don't think it's quite so easy to say in which month an unspecified day of the week in a particular numbered week might lie.


-- 
David.

> 
> Thanks for help,
> Janko
> 
> On Thu, Jan 12, 2017 at 8:37 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>> 
>>> On Jan 12, 2017, at 8:14 AM, Janko Thyson <janko.thyson at gmail.com> wrote:
>>> 
>>> Dear list,
>>> 
>>> I'm experiencing problems with converting strings of the format
>>> "YYYY-<weekofyear>" (e.g. 2016-01, 2016-52) to proper POSIX dates which (I
>>> think) I need in order to retrieve the month-of-the-year number.
>>> 
>>> Simpler put: I'd like to match week-of-the-year numbers to
>>> month-of-the-year numbers. Ideally, the week-of-the-year number would
>>> follow the ISO 8601 convention (i.e. format argument "%V") instead of the
>>> US (format argument "%U") or UK (format argument "%W") convention.
>>> 
>>> After posting this to Stackoverflow, I have strong reasons to believe that
>>> the issue is caused by Windows:
>>> http://stackoverflow.com/questions/41616407/match-iso-8601-week-numbers-to-month-of-year-on-windows-with-german-locale/41617215?noredirect=1#comment70436768_41617215
>>> 
>>> Example:
>>> 
>>> # ISO 8601 convention:
>>> 
>>> (yw <- format(posix, "%Y-%V"))
>> 
>> The documentation for R datetime format parameters ?strptime says %V is ignored on input.
>> 
>> 
>>> # [1] "2015-52" "2015-53" "2016-53" "2016-01"
>>> ywd <- sprintf("%s-1", yw)(as.POSIXct(ywd, format = "%Y-%V-%u"))
>> 
>> The documentation for R datetime format parameters ( = ?strptime) says %V is ignored on input.
>> 
>> You should leartn to post plain text to r-help.
>> 
>> --
>> David.
>> 
>> 
>>> # [1]
>>> "2015-01-12 CET" "2015-01-12 CET" "2016-01-12 CET" "2016-01-12 CET"#
>>> -> utterly wrong!!!
>>> 
>>> # US convention:
>>> (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 00A fellow R user tested this on both macOS and
>>> Ubuntu and he didn't encounter the issue:
>>> 
>>> 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"
>>> 
>>> My session info:
>>> 
>>>> 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
>>> 
>>> Any idea on how to workaround this issue on Windows?
>>> 
>>> Thanks and best regards,
>>> 
>>> Janko Thyson
>>> 
>>>      [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>> 
>> David Winsemius
>> Alameda, CA, USA
>> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list