uncount
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/uncount.html
%run nb_helpers.py
from datar.all import *
nb_header(uncount)
# https://tidyr.tidyverse.org/reference/uncount.html
%run nb_helpers.py
from datar.all import *
nb_header(uncount)
Try this notebook on binder.
★ uncount¶
Duplicating rows according to a weighting variable¶
Args:¶
data: A data frame
weights: A vector of weights. Evaluated in the context of data
_remove: If TRUE, and weights is the name of a column in data,
then this column is removed.
_id: Supply a string to create a new variable which gives a
unique identifier for each created row (0-based).
Returns:¶
dataframe with rows repeated.
In [2]:
Copied!
df = tibble(x = c("a", "b"), n = c(1, 2))
df >> uncount(f.n)
df = tibble(x = c("a", "b"), n = c(1, 2))
df >> uncount(f.n)
Out[2]:
| x | |
|---|---|
| <object> | |
| 0 | a |
| 1 | b |
| 2 | b |
In [3]:
Copied!
df >> uncount(f.n, _id="id")
df >> uncount(f.n, _id="id")
Out[3]:
| id | x | |
|---|---|---|
| <int64> | <object> | |
| 0 | 0 | a |
| 1 | 1 | b |
| 2 | 1 | b |
In [4]:
Copied!
uncount(df, 2)
uncount(df, 2)
Out[4]:
| x | n | |
|---|---|---|
| <object> | <int64> | |
| 0 | a | 1 |
| 1 | a | 1 |
| 2 | b | 2 |
| 3 | b | 2 |
In [5]:
Copied!
df >> uncount(2//f.n)
df >> uncount(2//f.n)
Out[5]:
| x | |
|---|---|
| <object> | |
| 0 | a |
| 1 | a |
| 2 | b |
In [ ]:
Copied!