Skip to contents

This function verifies the consistency between downloaded Comex data and the corresponding conference files provided by the official source (MDIC). It checks if the aggregated totals for specific columns match between the two sources.

Usage

comex_check(years = NULL, directions = NULL, type = "ncm")

Arguments

years

A numeric vector specifying the years to check. If NULL (default), all available years are checked.

directions

A character vector specifying the directions of trade to check: 'imp' (imports) and/or 'exp' (exports). If NULL (default), both directions are checked.

type

A character string indicating the type of Comex data: 'ncm' (Nomenclatura Comum do Mercosul) or 'hs4' (Harmonized System 4-digit). Defaults to 'ncm'.

Value

No direct return value. The function prints a message indicating whether the check passed ('All clear!') or if there are mismatches between the data and conference files. If mismatches are found, a table displaying the discrepancies is printed and an error is raised.

Details

This function performs the following checks:

  1. Input Validation: Ensures that the type argument is valid ('ncm' or 'hs4') and has a length of 1.

  2. Data Retrieval: Reads the relevant Comex data (based on the type) and the corresponding conference files.

  3. Filtering: Filters the data and conference files based on the specified years and directions if provided.

  4. Aggregation: Aggregates the Comex data by year and direction and calculates sums for relevant columns.

  5. Comparison: Compares the aggregated sums from the Comex data with the totals in the conference files.

  6. Mismatch Reporting: If any mismatches are found, a table highlighting the differences is printed to the console.

  7. Error Handling: If mismatches exist, the function raises an error to stop further analysis.

Examples

if (FALSE) { # \dontrun{
# Check consistency for all NCM data from 2020 onwards
comex_check(years = 2020:2023)

# Check consistency for only import data (NCM)
comex_check(directions = 'imp')

# Check consistency for HS4 data in 2022
comex_check(years = 2022, type = 'hs4')
} # }