cas_check: Check CAS format and checksum

View source: R/cas_check.R

cas_checkR Documentation

Check CAS format and checksum

Description

One-step check for CAS, via format and checksum; optionally preprocess input

Usage

cas_check(x, preprocess = TRUE, removeWS = TRUE)

Arguments

x

chr. A vector of values to check. Standard CAS notation using hyphens is fine, as all non-digit characters are stripped for checksum calculation

preprocess

logi. Trim leading and trailing whitespace and pare all consecutive (-{2,}) to (-)? Defaults to TRUE

removeWS

logi. Should all whitespace, be removed? Defaults to TRUE

Details

This is mainly a convenience function that combines cas_detect and cas_checkSum with an additional parameter to handle whitespaces. The use case for cas_detect is broader than this function, and therefore an argument to explicitly handle whitespaces is not included there, but is here.

Do note that because this implements both format and checksum checks, it is more stringent than either standalone function, which may or may not be desirable. Please see the related functions if you require more granular control.

Value

A logical vector of length x denoting whether each x is a valid CAS by both format and the last-digit checksum calculation NA input values will remain NA.

Note

The standalone functions are natively vectorized, and since this encompasses the full functionality of is.cas, it is arguably a vectorized alternative.

See Also

Other cas_functions: cas_checkSum(), cas_detect()

Examples

# preprocessing demo
x_casOnly <- c("598-42-5", "19438-61-0", "20730-39-6") # all 'real' CAS RNs
x_messy   <- c("casrn:  598-42-5", "19438-61 - 0", "20730--39-6")
cas_check(x_casOnly, preprocess = FALSE) # already known good, preprocess not needed
cas_check(x_messy, preprocess = FALSE) # removeWS not enough to handle '--'
# NA handling
cas_check(c(NA, x_messy, NA)) # NA passed through
# NULL handling
cas_check(c(NA, NULL, x_messy, NULL, NA)) # NULL removed, length == length(x)-length(is.null(x))

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.