Patrick Giraudoux
2021-Feb-22 14:26 UTC
[R] strptime, date and conversion of week number into POSIX
Dear all, I have a trouble trying to convert dates? given in character to POSIX. The date is expressed as a year then the week number e.g. "2020-01" (first week of 2020). I thought is can be converted as following: strptime(mydate,format="%Y-%W") %W refering to the 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), as indicated in the doc. However, I got this result, with the month fixed to 02 (february) and day 22 (only the year is? converted correctly): strptime(mydate,format="%Y-%W") [1] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [5] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [9] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [13] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [17] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [21] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [25] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [29] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [33] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [37] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [41] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [45] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [49] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [53] "2020-02-22 CET" "2021-02-22 CET" "2021-02-22 CET" "2021-02-22 CET" [57] "2021-02-22 CET" "2021-02-22 CET" "2021-02-22 CET" You'll find below a dump of "mydate" you can copy and paster if you need a try Any hint welcome... Best, Patrick mydate <- c("2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06", "2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12", "2020-13", "2020-14", "2020-15", "2020-16", "2020-17", "2020-18", "2020-19", "2020-20", "2020-21", "2020-22", "2020-23", "2020-24", "2020-25", "2020-26", "2020-27", "2020-28", "2020-29", "2020-30", "2020-31", "2020-32", "2020-33", "2020-34", "2020-35", "2020-36", "2020-37", "2020-38", "2020-39", "2020-40", "2020-41", "2020-42", "2020-43", "2020-44", "2020-45", "2020-46", "2020-47", "2020-48", "2020-49", "2020-50", "2020-51", "2020-52", "2020-53", "2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06") [[alternative HTML version deleted]]
Patrick Giraudoux
2021-Feb-22 15:15 UTC
[R] strptime, date and conversion of week number into POSIX
Sorry to answer to myself, but the format was clearly incorrect in the previous post. It should read, refering to the 1th day of the week: strptime(paste0(mydate,"-1"),format="%Y-%W-%u") It converts better, but with a NA on week 53> strptime(paste0(pays$year_week,"-1"),format="%Y-%W-%u")[1] "2020-01-06 CET" "2020-01-13 CET" "2020-01-20 CET" "2020-01-27 CET" [5] "2020-02-03 CET" "2020-02-10 CET" "2020-02-17 CET" "2020-02-24 CET" [9] "2020-03-02 CET" "2020-03-09 CET" "2020-03-16 CET" "2020-03-23 CET" [13] "2020-03-30 CEST" "2020-04-06 CEST" "2020-04-13 CEST" "2020-04-20 CEST" [17] "2020-04-27 CEST" "2020-05-04 CEST" "2020-05-11 CEST" "2020-05-18 CEST" [21] "2020-05-25 CEST" "2020-06-01 CEST" "2020-06-08 CEST" "2020-06-15 CEST" [25] "2020-06-22 CEST" "2020-06-29 CEST" "2020-07-06 CEST" "2020-07-13 CEST" [29] "2020-07-20 CEST" "2020-07-27 CEST" "2020-08-03 CEST" "2020-08-10 CEST" [33] "2020-08-17 CEST" "2020-08-24 CEST" "2020-08-31 CEST" "2020-09-07 CEST" [37] "2020-09-14 CEST" "2020-09-21 CEST" "2020-09-28 CEST" "2020-10-05 CEST" [41] "2020-10-12 CEST" "2020-10-19 CEST" "2020-10-26 CET" "2020-11-02 CET" [45] "2020-11-09 CET" "2020-11-16 CET" "2020-11-23 CET" "2020-11-30 CET" [49] "2020-12-07 CET" "2020-12-14 CET" "2020-12-21 CET" "2020-12-28 CET" [53] NA "2021-01-04 CET" "2021-01-11 CET" "2021-01-18 CET" [57] "2021-01-25 CET" "2021-02-01 CET" "2021-02-08 CET" Warning message: In strptime(paste0(pays$year_week, "-1"), format = "%Y-%W-%u") : (0-based) yday 369 in year 2020 is invalid Any idea on how to handle this ? Le 22/02/2021 ? 15:26, Patrick Giraudoux a ?crit?:> > Dear all, > > I have a trouble trying to convert dates? given in character to POSIX. > The date is expressed as a year then the week number e.g. "2020-01" > (first week of 2020). I thought is can be converted as following: > > strptime(mydate,format="%Y-%W") > > %W refering to the 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), as indicated in the doc. > > However, I got this result, with the month fixed to 02 (february) and > day 22 (only the year is? converted correctly): > > strptime(mydate,format="%Y-%W") [1] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [5] "2020-02-22 CET" "2020-02-22 > CET" "2020-02-22 CET" "2020-02-22 CET" [9] "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [13] "2020-02-22 > CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [17] > "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" > [21] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 > CET" [25] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" [29] "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 > CET" "2020-02-22 CET" [33] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [37] "2020-02-22 CET" "2020-02-22 > CET" "2020-02-22 CET" "2020-02-22 CET" [41] "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [45] "2020-02-22 > CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" [49] > "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" "2020-02-22 CET" > [53] "2020-02-22 CET" "2021-02-22 CET" "2021-02-22 CET" "2021-02-22 > CET" [57] "2021-02-22 CET" "2021-02-22 CET" "2021-02-22 CET" > > You'll find below a dump of "mydate" you can copy and paster if you > need a try > > Any hint welcome... > > Best, > > Patrick > > mydate <- > c("2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06", > "2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12", > "2020-13", "2020-14", "2020-15", "2020-16", "2020-17", "2020-18", > "2020-19", "2020-20", "2020-21", "2020-22", "2020-23", "2020-24", > "2020-25", "2020-26", "2020-27", "2020-28", "2020-29", "2020-30", > "2020-31", "2020-32", "2020-33", "2020-34", "2020-35", "2020-36", > "2020-37", "2020-38", "2020-39", "2020-40", "2020-41", "2020-42", > "2020-43", "2020-44", "2020-45", "2020-46", "2020-47", "2020-48", > "2020-49", "2020-50", "2020-51", "2020-52", "2020-53", "2021-01", > "2021-02", "2021-03", "2021-04", "2021-05", "2021-06") >[[alternative HTML version deleted]]
Bert Gunter
2021-Feb-22 16:04 UTC
[R] strptime, date and conversion of week number into POSIX
I think the relevant info from ?strptime is: "For strptime the input string need not specify the date completely: it is assumed that unspecified seconds, minutes or hours are zero, and an unspecified year, month or day is the current one. (However, if a month is specified, the day of that month has to be specified by %d or %e since the current day of the month need not be valid for the specified month.) Some components may be returned as NA (but an unknown tzone component is represented by an empty string)." I think this means that your strings cannot be converted by strptime() as essential info is missing. Input from those with more date-time expertise on this point would be welcome, however. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Feb 22, 2021 at 6:27 AM Patrick Giraudoux < patrick.giraudoux at univ-fcomte.fr> wrote:> Dear all, > > I have a trouble trying to convert dates given in character to POSIX. > The date is expressed as a year then the week number e.g. "2020-01" > (first week of 2020). I thought is can be converted as following: > > strptime(mydate,format="%Y-%W") > > %W refering to the 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), as indicated in the doc. > > However, I got this result, with the month fixed to 02 (february) and > day 22 (only the year is converted correctly): > > strptime(mydate,format="%Y-%W") [1] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [5] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [9] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [13] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [17] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [21] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [25] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [29] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [33] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [37] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [41] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [45] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [49] "2020-02-22 CET" "2020-02-22 CET" > "2020-02-22 CET" "2020-02-22 CET" [53] "2020-02-22 CET" "2021-02-22 CET" > "2021-02-22 CET" "2021-02-22 CET" [57] "2021-02-22 CET" "2021-02-22 CET" > "2021-02-22 CET" > > You'll find below a dump of "mydate" you can copy and paster if you need > a try > > Any hint welcome... > > Best, > > Patrick > > mydate <- > c("2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06", > "2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12", > "2020-13", "2020-14", "2020-15", "2020-16", "2020-17", "2020-18", > "2020-19", "2020-20", "2020-21", "2020-22", "2020-23", "2020-24", > "2020-25", "2020-26", "2020-27", "2020-28", "2020-29", "2020-30", > "2020-31", "2020-32", "2020-33", "2020-34", "2020-35", "2020-36", > "2020-37", "2020-38", "2020-39", "2020-40", "2020-41", "2020-42", > "2020-43", "2020-44", "2020-45", "2020-46", "2020-47", "2020-48", > "2020-49", "2020-50", "2020-51", "2020-52", "2020-53", "2021-01", > "2021-02", "2021-03", "2021-04", "2021-05", "2021-06") > > > [[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. >[[alternative HTML version deleted]]