Skip to content

Commit

Permalink
Make uncount() generic (#1358)
Browse files Browse the repository at this point in the history
* Make `uncount()` generic

* Document dots for `uncount()`

* NEWS bullet

* Move `check_dots_used()` into `uncount()` generic.
  • Loading branch information
mgirlich authored May 11, 2022
1 parent 2cffc2d commit 1b75df8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
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.

0 comments on commit 1b75df8

Please sign in to comment.