[R] strptime, date and conversion of week number into POSIX
Patrick Giraudoux
p@tr|ck@g|r@udoux @end|ng |rom un|v-|comte@|r
Mon Feb 22 15:26:48 CET 2021
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]]
More information about the R-help
mailing list