[R] Match ISO 8601 week-of-year numbers to month-of-year numbers on Windows with German locale
peter dalgaard
pdalgd at gmail.com
Sat Jan 14 10:17:11 CET 2017
> On 12 Jan 2017, at 22:20 , Nordlund, Dan (DSHS/RDA) <NordlDJ at dshs.wa.gov> wrote:
>
> See comments inline.
>
>> -----Original Message-----
>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Bob Rudis
>> Sent: Thursday, January 12, 2017 12:41 PM
>> To: David Winsemius
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Match ISO 8601 week-of-year numbers to month-of-year
>> numbers on Windows with German locale
>>
>> Aye, but this:
>>
>> 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"
>>
>> works fine on macOS & Linux (Ubuntu, anyway), but it fails on Windows (10,
>> 64bit, R 3.3.2):
>>
>> (as.POSIXct(year_week_day, format = "%Y-%U-%u"))
>> ## [1] "2015-12-21 PST" "2015-12-28 PST" NA "2016-01-04 PST"
>
> Why do you say it works fine on Ubuntu? The date "2016-01-01" is in week 0 of 2016. There is no Monday in week 0. So I would argue the return value of NA from Windows is more appropriate than returning the date of Monday in week 1 of 2016 like Ubuntu does.
Yes, the Ubuntu output suggests that Jan 4 is Monday of both w1 and w0...!
But I'm puzzled: Around here, we actually use week numbers (for planning), but they are never zero:
2016-01-01 was Friday, week 53 (of 2015)
2014-12-31 was Wednesday, week 1 (still 2015).
So it would seem that any logic that converts dates to/from week numbers would need to be rather more careful than to try and use %Y for the year...
-pd
>
> Dan
>
> Daniel Nordlund, PhD
> Research and Data Analysis Division
> Services & Enterprise Support Administration
> Washington State Department of Social and Health Services
>
>>
>> On 1/12/17, 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-
>> numbe
>>>> rs-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
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list