key_generator: Generate unique keys for dataset

Description Usage Arguments Value Examples

View source: R/key_generator.R

Description

This function generates a unique set of alphanumeric keys that can be assigned to a data.frame or other dataset. The code is adapted from that of Julien Navarre in this Stack Overflow thread. Parameters match those of the stri_rand_strings function.

Usage

1
key_generator(n, key_length, ...)

Arguments

n

Integer indicating the desired number of keys to generate.

key_length

Integer indicating the desired length of they key strings.

...

Additional arguments passed on to stri_rand_strings (e.g., a pattern argument specifying character classes from which elements will be drawn).

Value

Character vector of length n containing unique alphanumeric keys.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
key_generator(n=10, key_length = 6)
key_generator(n=5, key_length = 11)
key_generator(n=15, key_length = 3)

## Add key to data.frame
df <- data.frame(x=letters[1:10], y = 1:10)
df <- cbind(
  data.frame(key = key_generator(n=nrow(df), key_length = 3)),
  df)
df

tfullman/packr documentation built on Jan. 2, 2022, 12:16 a.m.