Performs the opposite operation to dplyr::count()
, duplicating rows
according to a weighting variable (or expression).
uncount(data, weights, .remove = TRUE, .id = NULL)
data | A data frame, tibble, or grouped tibble. |
---|---|
weights | A vector of weights. Evaluated in the context of |
.remove | If |
.id | Supply a string to create a new variable which gives a unique identifier for each created row. |
#> # A tibble: 3 x 1 #> x #> <chr> #> 1 a #> 2 b #> 3 buncount(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