peter salzman
2018-Oct-16 14:22 UTC
[R] year and week to date - before 1/1 and after 12/31
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]]
peter salzman
2018-Oct-16 15:11 UTC
[R] year and week to date - before 1/1 and after 12/31
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 at 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]]
Jeff Newmiller
2018-Oct-16 17:22 UTC
[R] year and week to date - before 1/1 and after 12/31
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 at 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 at 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.
Gabor Grothendieck
2018-Oct-18 12:26 UTC
[R] year and week to date - before 1/1 and after 12/31
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 at 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 at 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