sqids: Generate random-looking IDs from integer ranks

View source: R/sqids.R

sqidsR Documentation

Generate random-looking IDs from integer ranks

Description

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().

Usage

sqids(
  x,
  .salt = sample.int(1000, 3),
  .ties = c("sequential", "min", "max", "dense")
)

unsqids(x)

Arguments

x

For sqids(), a vector to rank. You can leave this argument missing to refer to the "current" row number in 'dplyr' verbs.

For unsqids(), a character vector of IDs.

.salt

Integers to use with each value of x to generate IDs.

.ties

Method to rank duplicate values. One of "sequential", "min", "max", or "dense". See ties argument of vctrs::vec_rank() for more details.

Value

For sqids(), a character vector of IDs.

For unsqids(), integers.

See Also

sqids/sqids-cpp

Examples

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)

paithiov909/ldccr documentation built on Feb. 3, 2025, 12:16 a.m.