Closed andrewallenbruce closed 8 months ago
Could create an NPI constructor, ala:
``` r library(provider) library(dplyr) library(purrr) library(rlang) create_npi <- function(npi, arg = rlang::caller_arg(npi), call = rlang::caller_env()) { # Must be numeric if (grepl("^[[:digit:]]+$", npi) == FALSE) { cli::cli_abort(c( "An {.strong NPI} must be {.emph numeric}.", "x" = "{.val {npi}} contains {.emph non-numeric} characters."), call = call) } # Must be 10 char length if (nchar(npi) != 10L) { cli::cli_abort(c( "An {.strong NPI} must be {.emph 100 digits long}.", "x" = "{.val {npi}} contains {.val {nchar(npi)}} digit{?s}."), call = call) } # Must pass Luhn algorithm npi_test <- as.character(npi) # Remove the 10th digit to create the 9-position identifier part of the NPI id <- unlist(strsplit(npi_test, ""), use.names = FALSE)[1:9] # Reverse order of digits x <- rev(id) # Select index of every other digit idx <- seq(1, length(x), 2) # Double the value of the alternate digits x[idx] <- as.numeric(x[idx]) * 2 # Reverse order of digits again x <- rev(x) # Split and unlist to separate digits x <- unlist(strsplit(x, ""), use.names = FALSE) # Add constant 24 to the sum of the digits x <- sum(as.numeric(x)) + 24 # Find the next higher number ending in zero y <- ceiling(x / 10) * 10 # Find the check digit by subtracting x from y z <- y - x # Append the check digit to the end of the 9-digit identifier id[10] <- z # Collapse the vector into a single string npi_valid <- paste0(id, collapse = "") results <- list( input = npi_test, valid = npi_valid, equal = identical(npi_valid, npi_test)) return(results) } ```
# invalid
create_npi(1234567891)
#> $input
#> [1] "1234567891"
#>
#> $valid
#> [1] "1234567893"
#>
#> $equal
#> [1] FALSE
create_npi(1528060439)
#> $input
#> [1] "1528060439"
#>
#> $valid
#> [1] "1528060431"
#>
#> $equal
#> [1] FALSE
# valid
create_npi(1528060837)
#> $input
#> [1] "1528060837"
#>
#> $valid
#> [1] "1528060837"
#>
#> $equal
#> [1] TRUE
Created on 2023-10-23 with reprex v2.0.2
check_npi()
calls with validate_npi()
affiliations()
clinicians()
hospitals()
nppes()
open_payments()
opt_out()
order_refer()
pending()
providers()
quality_payment()
quality_eligibility()
mips_2021()
reassignments()
utilization()
Luhn Formula for Modulus 10 “double-add-double” Check Digit
Example of Check Digit Calculation for NPI used without Prefix:
Created on 2023-10-12 with reprex v2.0.2