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

Make uncount() generic #1358

Merged
merged 4 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ S3method(separate_rows,data.frame)
S3method(separate_rows_,data.frame)
S3method(spread,data.frame)
S3method(spread_,data.frame)
S3method(uncount,data.frame)
S3method(unite,data.frame)
S3method(unite_,data.frame)
S3method(unnest,data.frame)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# tidyr (development version)

* `uncount()` is now generic so implementations can be provided for objects
other than data frames (@mgirlich, #1358).

* `uncount()` gained the `...` argument. It comes between the required and the
optional arguments (@mgirlich, #1358).

* `pivot_longer()` gained a new `cols_vary` argument for controlling the
ordering of the output rows relative to their original row number (#1312).

Expand Down
9 changes: 8 additions & 1 deletion R/uncount.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param data A data frame, tibble, or grouped tibble.
#' @param weights A vector of weights. Evaluated in the context of `data`;
#' supports quasiquotation.
#' @param ... Additional arguments passed on to methods.
#' @param .id Supply a string to create a new variable which gives a unique
#' identifier for each created row.
#' @param .remove If `TRUE`, and `weights` is the name of a column in `data`,
Expand All @@ -21,7 +22,13 @@
#'
#' # Or expressions
#' uncount(df, 2 / n)
uncount <- function(data, weights, .remove = TRUE, .id = NULL) {
uncount <- function(data, weights, ..., .remove = TRUE, .id = NULL) {
check_dots_used()
UseMethod("uncount")
}

#' @export
uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) {
weights_quo <- enquo(weights)
w <- dplyr::pull(dplyr::mutate(data, `_weight` = !!weights_quo))
# NOTE `vec_cast()` and check for positive weights can be removed
Expand Down
4 changes: 3 additions & 1 deletion man/uncount.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.