Learn R Programming

epidm (version 1.0.6)

valid_nhs: NHS Number Validity Check

Description

[Stable]

Validates NHS numbers using the NHS checksum applied to the first 9 digits and compared with the 10th digit. Inputs that are not exactly 10 numeric characters are invalid. Numbers made of the same repeated digit (e.g., "1111111111", "0000000000") are also rejected.

Usage

valid_nhs(nhs_number)

Value

A numeric vector of the same length as nhs_number containing:

  • 1 if the value is a valid NHS number

  • 0 otherwise

Arguments

nhs_number

A vector of values to validate. Each element is coerced to character for length checking and digit extraction.

Details

Algorithm (summary):

  1. Take the first 9 digits and multiply by weights 10 down to 2 (i.e., d1×10 + d2×9 + … + d9×2).

  2. Compute 11 - (sum %% 11) → this is the expected check digit.

  3. If the expected check digit is 11, treat as 0.

  4. Compare with the 10th digit. If they match, the number is valid.

Additional guards implemented:

  • If the NHS number is NA or not 10 characters, it is invalid.

  • If all 10 digits are identical (e.g., "1111111111"), it is invalid.

The function is vectorised and returns 1 for valid and 0 for invalid for each element in the input vector.

Examples

Run this code
test <- floor(runif(1000,1000000000,9999999999))
valid_nhs(test)
valid_nhs(9434765919)

Run the code above in your browser using DataLab