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

peter salzman peter@@@|zm@nu@er @end|ng |rom gm@||@com
Tue Oct 16 17:11:12 CEST 2018


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
>


-- 
Peter Salzman, PhD
Department of Biostatistics and Computational Biology
University of Rochester

	[[alternative HTML version deleted]]




More information about the R-help mailing list