sqids | R Documentation |
sqids()
is an alternative to dplyr::row_number()
that generates random-looking IDs from integer ranks
using Sqids (formerly Hashids).
IDs that generated with sqids()
can be easily decoded back into
the original ranks using unsqids()
.
sqids(
x,
.salt = sample.int(1000, 3),
.ties = c("sequential", "min", "max", "dense")
)
unsqids(x)
x |
For For |
.salt |
Integers to use with each value of |
.ties |
Method to rank duplicate values.
One of |
For sqids()
, a character vector of IDs.
For unsqids()
, integers.
ids <- sqids(c(5, 1, 3, 2, 2, NA))
ids
unsqids(ids)
df <- data.frame(
grp = c(1, 1, 1, 2, 2, 2, 3, 3, 3)
)
# You can use `sqids()` without referencing `x` in dplyr verbs.
dplyr::mutate(df, sqids = sqids(), row_id = unsqids(sqids))
# Use `.ties` to control how to rank duplicate values.
dplyr::mutate(df, sqids = sqids(grp, .ties = "min"), grp_id = unsqids(sqids))
# When you need to generate the same IDs for each group, fix the `.salt`:
dplyr::mutate(df, sqids = sqids(.salt = 1234L), .by = grp)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.