[R] strptime, date and conversion of week number into POSIX

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Feb 22 17:04:28 CET 2021


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 using 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 using 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]]



More information about the R-help mailing list