Skip to content
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

Refactors quarter() API to allow for quarter start- and end-dates #955

Merged
merged 4 commits into from
Apr 8, 2021
Merged

Refactors quarter() API to allow for quarter start- and end-dates #955

merged 4 commits into from
Apr 8, 2021

Conversation

tomcardoso
Copy link
Contributor

Addresses #947. With this, the API now looks like:

quarter(x) # default behaviour is unchanged
quarter(x, type = "quarter_year") # equivalent to what would previously be passing `with_year = TRUE`
quarter(x, type = "date_first") # new functionality
quarter(x, type = "date_last") # new functionality

The with_year parameter is soft-deprecated, but will continue to work for the time being.

@tomcardoso
Copy link
Contributor Author

One thing: I tried running pkgdown::build_site() but got an error about tidytemplate not being installed. Is rebuilding docs something that happens separately from the PR?

@DavisVaughan
Copy link
Member

DavisVaughan commented Mar 3, 2021

FWIW these capabilities come built into the upcoming clock package, where the year-quarter-day calendar is a first class type.

library(clock)
library(lubridate)

x <- as.Date("2019-04-25")

yqd1 <- as_year_quarter_day(x)
yqd1
#> <year_quarter_day<January><day>[1]>
#> [1] "2019-Q2-25"

# Get the quarter
quarter(x)
#> [1] 2
get_quarter(yqd1)
#> [1] 2

# Change the fiscal start month and try again
yqd2 <- as_year_quarter_day(x, start = clock_months$april)
yqd2
#> <year_quarter_day<April><day>[1]>
#> [1] "2020-Q1-25"

quarter(x, fiscal_start = 4)
#> [1] 1
get_quarter(yqd2)
#> [1] 1

# Get the year+quarter
quarter(x, type = "quarter_year")
#> [1] 2019.2
paste0(get_year(yqd1), ".", get_quarter(yqd1))
#> [1] "2019.2"

# or, more appropriately:
calendar_narrow(yqd1, "quarter")
#> <year_quarter_day<January><quarter>[1]>
#> [1] "2019-Q2"

# Get the first day of the quarter
quarter(x, type = "date_first")
#> [1] "2019-04-01"
as.Date(set_day(yqd1, 1))
#> [1] "2019-04-01"

# Get the last day of the quarter
quarter(x, type = "date_last")
#> [1] "2019-06-30"
as.Date(set_day(yqd1, "last"))
#> [1] "2019-06-30"

Created on 2021-03-03 by the reprex package (v1.0.0)

@tomcardoso
Copy link
Contributor Author

@DavisVaughan Neat! Is the idea that clock would supersede lubridate eventually, since it's coming from the r-lib folks?

@DavisVaughan
Copy link
Member

Not really supersede. Instead we currently are thinking of it as an alternative to lubridate, with lubridate still living on and being a powerful tool for working with date-times. In the far future, bits and pieces of lubridate (like this, for example) might end up relying on clock, but that is still a little uncertain.

@tomcardoso
Copy link
Contributor Author

Hey @vspinu, any thoughts on this? Let me know if you want me to tweak anything.

@vspinu
Copy link
Member

vspinu commented Mar 28, 2021

Sorry for delaying on this. There was an issue with the logical check which could fail for non-logical inputs. Also I don't quite like quarter_year because the return values is actually of the form year.quarter. So I have renamed that type. Please see my amendments. If it's ok with you I will merge.

@vspinu vspinu merged commit 35d167d into tidyverse:master Apr 8, 2021
@vspinu
Copy link
Member

vspinu commented Apr 8, 2021

@tomcardoso you might have missed my last message. If you disagree with my amendments please let me know.

@tomcardoso
Copy link
Contributor Author

Oh shoot, sorry @vspinu, I did miss this. Makes total sense to me – thanks for talking me through this stuff. Looking forward to putting the new functionality to use!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants