-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for arithmetic manipulation of aweek
objects
#27
Comments
I've thought about this before and didn't really know how best to handle it since the granularity of aweek objects goes down to the day level. At the moment, what you propose is currently possible via the following (albiet awkward) syntax: library("aweek")
set_week_start("Monday")
d <- Sys.Date() + (-3:3)
w <- as.aweek(d)
w
#> <aweek start: Monday>
#> [1] "2019-W29-3" "2019-W29-4" "2019-W29-5" "2019-W29-6" "2019-W29-7"
#> [6] "2019-W30-1" "2019-W30-2"
w2 <- as.aweek(as.Date(w) + (1 * 7))
w2
#> <aweek start: Monday>
#> [1] "2019-W30-3" "2019-W30-4" "2019-W30-5" "2019-W30-6" "2019-W30-7"
#> [6] "2019-W31-1" "2019-W31-2" Created on 2019-07-20 by the reprex package (v0.3.0) It's very possible to define What should the granularity be? If I do |
Would it be possible to have 2 separate granularities, depending on the object? It seems you have 2 possible objects, both of which it would be highly useful to manipulate - one which gives the day, and one which more usefully (IMO) gives the week: aweek::date2week(Sys.Date())
#> <aweek start: Monday>
#> [1] "2019-W29-7"
aweek::date2week(Sys.Date(), floor_day = TRUE)
#> <aweek start: Monday>
#> [1] "2019-W29" Created on 2019-07-21 by the reprex package (v0.3.0) Would it therefore be possible to do, for example: aweek::date2week(Sys.Date()) + 1
#> Error in aweek::date2week(Sys.Date()) + 1: non-numeric argument to binary operator
aweek::date2week(Sys.Date(), floor_day = TRUE) + 1
#> Error in aweek::date2week(Sys.Date(), floor_day = TRUE) + 1: non-numeric argument to binary operator Created on 2019-07-21 by the reprex package (v0.3.0) and, in the first instance, add a day, while, in the second, add a week? |
I don't think it's wise to have two separate granularities here because the YYYY-Www construct is merely shorthand for YYYY-Www-1, so both types are valid in the same vector: aweek::as.aweek(c("2019-W10-2", "2019-W20"))
#> <aweek start: Monday>
#> [1] "2019-W10-2" "2019-W20" Created on 2019-07-22 by the reprex package (v0.3.0) I think, given that the granularity for the object is a single day, I'll have the |
It would be great if one could work with
aweek
objects, e.g. by adding and subtracting to get the precedent or subsequent weeks, akin to what is possible with dates. Something likeaweek::date2week(Sys.Date()) + 1
The text was updated successfully, but these errors were encountered: