Skip to content

Commit 1b75df8

Browse files
authored
Make uncount() generic (#1358)
* Make `uncount()` generic * Document dots for `uncount()` * NEWS bullet * Move `check_dots_used()` into `uncount()` generic.
1 parent 2cffc2d commit 1b75df8

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ S3method(separate_rows,data.frame)
3232
S3method(separate_rows_,data.frame)
3333
S3method(spread,data.frame)
3434
S3method(spread_,data.frame)
35+
S3method(uncount,data.frame)
3536
S3method(unite,data.frame)
3637
S3method(unite_,data.frame)
3738
S3method(unnest,data.frame)

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# tidyr (development version)
22

3+
* `uncount()` is now generic so implementations can be provided for objects
4+
other than data frames (@mgirlich, #1358).
5+
6+
* `uncount()` gained the `...` argument. It comes between the required and the
7+
optional arguments (@mgirlich, #1358).
8+
39
* `pivot_longer()` gained a new `cols_vary` argument for controlling the
410
ordering of the output rows relative to their original row number (#1312).
511

R/uncount.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param data A data frame, tibble, or grouped tibble.
77
#' @param weights A vector of weights. Evaluated in the context of `data`;
88
#' supports quasiquotation.
9+
#' @param ... Additional arguments passed on to methods.
910
#' @param .id Supply a string to create a new variable which gives a unique
1011
#' identifier for each created row.
1112
#' @param .remove If `TRUE`, and `weights` is the name of a column in `data`,
@@ -21,7 +22,13 @@
2122
#'
2223
#' # Or expressions
2324
#' uncount(df, 2 / n)
24-
uncount <- function(data, weights, .remove = TRUE, .id = NULL) {
25+
uncount <- function(data, weights, ..., .remove = TRUE, .id = NULL) {
26+
check_dots_used()
27+
UseMethod("uncount")
28+
}
29+
30+
#' @export
31+
uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) {
2532
weights_quo <- enquo(weights)
2633
w <- dplyr::pull(dplyr::mutate(data, `_weight` = !!weights_quo))
2734
# NOTE `vec_cast()` and check for positive weights can be removed

man/uncount.Rd

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)