uncount: "Uncount" a data frame

Description Usage Arguments Examples

View source: R/uncount.R

Description

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

Usage

1

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 single

.id

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

Examples

1
2
3
4
5
6
7
8
9
df <- tibble::tibble(x = c("a", "b"), n = c(1, 2))
uncount(df, n)
uncount(df, n, .id = "id")

# You can also use constants
uncount(df, 2)

# Or expressions
uncount(df, 2 / n)

Example output

# A tibble: 3 x 1
  x    
  <chr>
1 a    
2 b    
3 b    
# A tibble: 3 x 2
  x        id
  <chr> <int>
1 a         1
2 b         1
3 b         2
# A tibble: 4 x 2
  x         n
  <chr> <dbl>
1 a         1
2 a         1
3 b         2
4 b         2
# A tibble: 3 x 2
  x         n
  <chr> <dbl>
1 a         1
2 a         1
3 b         2

tidyr documentation built on Oct. 29, 2018, 1:04 a.m.