knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

verhoeff

This package is my take on implementing the Verhoeff algorithim for calculating check digits. My influence for the algorithims implementation was primarily from the wikipedia outline (https://en.wikipedia.org/wiki/Verhoeff_algorithm), and the paper by A. Dunn (Computer program for the calculation and validation of Verhoeff check digits, https://www.ccamlr.org/en/wg-fsa-sam-05/11)

Installation

You can install verhoeff from CRAN with:

install.packages("verhoeff")

Or grab the development version from github with:

devtools::install_github("condwanaland/verhoeff")

Example

To calculate the Verhoeff Check Digit for a given number, use verhoeff_calculate

library(verhoeff)
verhoeff::verhoeff_calculate(123)

To calculate for many numbers, just pass a vector.

verhoeff_calculate(c(1234, 5678, 9, 10))

If you would prefer the output to be in a list, with one check digit per element, use the as_list parameter.

verhoeff_calculate(c(1234, 5678, 9, 10), as_list = TRUE)

The output of verhoeff_calculate is designed so it can be a new column in a dataframe (i.e., as the output of a dplyr::mutate call)

suppressPackageStartupMessages(library(dplyr))

# Make a random dataframe that has integer columns
mtcars$name <- rownames(mtcars)
mtcars %>% 
  select(name, gear) %>% 
  mutate(check_digit = verhoeff_calculate(gear))

In addition to verhoeff_calculate, verhoeff offers verhoeff_append and verhoeff_validate.

Use verhoeff_append to append a check digit to a provided number:

verhoeff_append(123)
verhoeff_append(c(123, 5, 0), sep = "_")

Use verhoeff_validate to check whether a check digit is correct for the provided number

verhoeff_validate(number = 123, check_digit = 3)
verhoeff_validate(number = 123, check_digit = 4)

Other implementations

See https://github.com/fascinatingfingers/CheckDigit for another implementation of check digits that considers more than just the Verhoeff algorithim.



condwanaland/verhoeff documentation built on Sept. 6, 2021, 7:20 p.m.