Skip to content

Instantly share code, notes, and snippets.

@danlewer
Last active February 19, 2024 19:01
Show Gist options
  • Save danlewer/08554e1da9770b6509f3803561f3dc6e to your computer and use it in GitHub Desktop.
Save danlewer/08554e1da9770b6509f3803561f3dc6e to your computer and use it in GitHub Desktop.
# Here's a function that allows you to specify the number of letters you want (eg. 5 would be A, B, C, D, and E), the length of the code (eg. 3 would be AAA, AAB, AAC, etc), the number of results you want (NA for all of them), and and separating character (eg. '-' would give A-A-A, A-A-B, A-A-C.)
letterCodes <- function(nletters, case = 'upper', lengthCode, nResults = NA, sep = '') {
f <- if (case == 'upper') LETTERS else letters
a <- expand.grid(rep(list(f[1:nletters]), lengthCode))
a <- a[, ncol(a):1]
a <- do.call("paste", c(a, sep = sep))
if (is.na(nResults)) a else a[1:min(nResults, length(a))]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment