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

Gabor Grothendieck ggrothend|eck @end|ng |rom gm@||@com
Thu Oct 18 14:26:12 CEST 2018


Replace the week in the date with week 2, say -- a week in which
nothing will go wrong and then add or subtract the appropriate number
of weeks.

  d <- c('2016 00 Sun', '2017 53 Sun', '2017 53 Mon') # test data

  as.Date(sub(" .. ", "02", d), "%Y %U %a") +
     7 * (as.numeric(sub(".... (..) ...", "\\1", d)) - 2)
  ## [1] "2015-12-27" "2017-12-31" "2018-01-01"

On Tue, Oct 16, 2018 at 10:23 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
>
>         [[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.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com




More information about the R-help mailing list