These are vectorized functions that take integer vectors and return Date or an aweek objects, making it easier to convert bare weeks to dates.
get_aweek( week = 1L, year = format(Sys.Date(), "%Y"), day = 1L, start = week_start, week_start = get_week_start(), ... ) get_date( week = 1L, year = format(Sys.Date(), "%Y"), day = 1L, start = get_week_start() )
week | an integer vector, defaults to 1, representing the first week of the year. |
---|---|
year | an integer vector, defaults to the current year |
day | an integer vector, defaults to 1, representing the first day of the first week of the year. |
start | an integer (or character) vector of days that the weeks
start on for each corresponding week. Defaults to the value of
|
week_start | a number indicating the start of the week based on the ISO
8601 standard from 1 to 7 where 1 = Monday OR an abbreviation of the
weekdate in an English or current locale. Note: using a non-English locale
may render your code non-portable. Defaults to the value of
|
... | parameters passed on to |
get_aweek(): an aweek object
get_date(): a Date object
Any missing weeks, years, or start elements will result in a missing element in the resulting vector. Any missing days will revert to the first day of the week.
# The default results in the first week of the year using the default # default week_start (from get_week_start()) get_aweek()#> <aweek start: Monday> #> [1] "2021-W01-1"get_date() # this is equivalent to as.Date(get_week()), but faster#> [1] "2021-01-04"# Some years, like 2015, have 53 weeks get_aweek(53, 2015)#> <aweek start: Monday> #> [1] "2015-W53-1"# If you specify 53 weeks for a year that doesn't have 53 weeks, aweek will # happily correct it for you get_aweek(53, 2014) # this will be 2015-W01-1#> <aweek start: Monday> #> [1] "2015-W01-1"# you can use this to quickly make a week without worrying about formatting # here, you can define an observation interval of 20 weeks obs_start <- get_date(week = 10, year = 2018) obs_end <- get_date(week = 29, year = 2018, day = 7) c(obs_start, obs_end)#> [1] "2018-03-05" "2018-07-22"# If you have a data frame of weeks, you can use it to convert easily mat <- matrix(c( 2019, 11, 1, 7, # 2019-03-10 2019, 11, 2, 7, 2019, 11, 3, 7, 2019, 11, 4, 7, 2019, 11, 5, 7, 2019, 11, 6, 7, 2019, 11, 7, 7 ), ncol = 4, byrow = TRUE) colnames(mat) <- c("year", "week", "day", "start") m <- as.data.frame(mat) m#> year week day start #> 1 2019 11 1 7 #> 2 2019 11 2 7 #> 3 2019 11 3 7 #> 4 2019 11 4 7 #> 5 2019 11 5 7 #> 6 2019 11 6 7 #> 7 2019 11 7 7#> [1] "2019-03-10" "2019-03-11" "2019-03-12" "2019-03-13" "2019-03-14" #> [6] "2019-03-15" "2019-03-16"#> <aweek start: Monday> #> [1] "2019-W10-7" "2019-W11-1" "2019-W11-2" "2019-W11-3" "2019-W11-4" #> [6] "2019-W11-5" "2019-W11-6"#> <aweek start: Sunday> #> [1] "2019-W11-1" "2019-W11-2" "2019-W11-3" "2019-W11-4" "2019-W11-5" #> [6] "2019-W11-6" "2019-W11-7"# You can also change starts mon <- with(m, get_aweek(week, year, day, "Monday", week_start = "Monday")) mon#> <aweek start: Monday> #> [1] "2019-W11-1" "2019-W11-2" "2019-W11-3" "2019-W11-4" "2019-W11-5" #> [6] "2019-W11-6" "2019-W11-7"#> [1] "2019-03-11" "2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15" #> [6] "2019-03-16" "2019-03-17"# If you use multiple week starts, it will convert to date and then to # the correct week, so it won't appear to match up with the original # data frame. sft <- with(m, get_aweek(week, year, day, 7:1, week_start = "Sunday")) sft#> <aweek start: Sunday> #> [1] "2019-W11-1" "2019-W11-1" "2019-W12-1" "2019-W12-1" "2019-W12-1" #> [6] "2019-W12-1" "2019-W12-1"#> [1] "2019-03-10" "2019-03-10" "2019-03-17" "2019-03-17" "2019-03-17" #> [6] "2019-03-17" "2019-03-17"