[R] year and week to date - before 1/1 and after 12/31

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Oct 16 20:11:42 CEST 2018


Er, my mistake, you are using %U not %W... but now I am really confused, because the first Sunday is trivial with %U/%u.

Can you clarify what your actual upstream input is? Is it an invalid date string as you say below, or is it year number?

On October 16, 2018 10:22:10 AM PDT, Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
>If the date in your character representation does not exist then there
>is no requirement for a POSIX function to give any reliable answer...
>including NA. Using 00 as the week number won't always work.
>
>The first week/weekday combination that is guaranteed to exist by POSIX
>is 1/1 (first Monday). If the corresponding mon/mday is 1/1 then no
>days exist in week zero for that year and the first Sunday is 6 days
>more than the mday of the first Monday, else the mday of the first
>Sunday is one day less than the mday of the first Monday.
>
>You should if at all possible repair the computations that are creating
>the invalid string dates you mention.
>
>On October 16, 2018 8:11:12 AM PDT, peter salzman
><peter.salzmanuser using gmail.com> wrote:
>>it is simpler than i thought
>>
>>first day of given week is the last day minus 6days
>>
>>in other words:
>>d1 = as.Date('2018 00 Sat',format="%Y %U %a") - 6
>>d2 = as.Date('2018 00 Sun',format="%Y %U %a")
>>are the same as long both are not NA
>>
>>therefore to get the one that is not NA one can do
>>
>>max( c(d1,d2), na.rm=TRUE )
>>
>>maybe there is some other trick
>>
>>best,
>>peter
>>
>>
>>
>>
>>
>>
>>On Tue, Oct 16, 2018 at 10:22 AM peter salzman
>><peter.salzmanuser using gmail.com>
>>wrote:
>>
>>> hi,
>>>
>>> to turn year and week into the date one can do the following:
>>>
>>> as.Date('2018 05 Sun', "%Y %W %a")
>>>
>>> however, when we want the Sunday (1st day of week) of the 1st week
>of
>>2018
>>> we get NA because 1/1/2018 was on Monday
>>>
>>> as.Date('2018 00 Mon',format="%Y %U %a")
>>> ## 2018-01-01
>>> as.Date('2018 00 Sun',format="%Y %U %a")
>>> ## NA
>>>
>>> btw the same goes for last week
>>> as.Date('2017 53 Sun',format="%Y %U %a")
>>> ## 2017-12-31
>>> as.Date('2017 53 Mon',format="%Y %U %a")
>>> ## NA
>>>
>>> So my question is :
>>> how do i get
>>> from "2018 00 Sun" to 2018-12-31
>>> and
>>> from "2017 53 Mon" to 2018-01-01
>>>
>>> i realize i can loop over days of week and do some if/then
>>statements,
>>> but is there a built in function?
>>>
>>> thank you
>>> peter
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Peter Salzman, PhD
>>> Department of Biostatistics and Computational Biology
>>> University of Rochester
>>>

-- 
Sent from my phone. Please excuse my brevity.




More information about the R-help mailing list