Skip to contents

This function cleans and optionally validates NCM (Nomenclatura Comum do Mercosul) codes.

Usage

ncm(x, nchar = 8, checkncm = TRUE)

Arguments

x

A character vector containing NCM codes.

checkncm

A logical flag indicating whether to perform NCM validation. Defaults to TRUE.

Value

A character vector of cleaned NCM codes.

  • Non-numeric characters are removed.

  • Empty strings are converted to NA.

  • If checkncm is TRUE, an error is raised if any non-NA values do not have exactly 8 characters.

Details

NCM codes are standardized to contain only digits (0-9). This is important for consistency in data analysis and comparison.

The optional validation step ensures that the cleaned NCM codes adhere to the expected format of 8 digits.

Examples

# Clean and validate valid NCM codes
ncm(c('01012100', '02011000', 'invalid code'))  # Raises an error due to 'invalid code'
#> [1] "01012100" "02011000" NA        

# Clean without validation
ncm(c('01012100', '02011000', 'invalid code'), checkncm = FALSE)
#> [1] "01012100" "02011000" NA        
# Returns: c('01012100', '02011000', NA)