Performs the opposite opperation to dplyr::count(), duplicating rows acoording to a weighting variable (or expression).

uncount(data, weights, .remove = TRUE, .id = NULL)

Arguments

data

A data frame, tibble, or grouped tibble.

weights

A vector of weights. Evaluated in the context of data; supports quasiquotation.

.remove

If TRUE, and weights is a signle `

.id

Supply a string to create a new variable which gives a unique identifier for each created row.

Examples

df <- tibble::tibble(x = c("a", "b"), n = c(1, 2)) uncount(df, n)
#> # A tibble: 3 x 1 #> x #> <chr> #> 1 a #> 2 b #> 3 b
uncount(df, n, .id = "id")
#> # A tibble: 3 x 2 #> x id #> <chr> <int> #> 1 a 1 #> 2 b 1 #> 3 b 2
# You can also use constants uncount(df, 2)
#> # A tibble: 4 x 2 #> x n #> <chr> <dbl> #> 1 a 1. #> 2 a 1. #> 3 b 2. #> 4 b 2.
# Or expressions uncount(df, 2 / n)
#> # A tibble: 3 x 2 #> x n #> <chr> <dbl> #> 1 a 1. #> 2 a 1. #> 3 b 2.