Skip to contents

This function aggregates a Comex (Brazilian trade) dataset by calculating the sum of columns whose names start with any of the specified prefixes.

Usage

comex_sum(
  data,
  x = c("qt_stat", "kg_net", "fob_", "freight_", "insurance_", "cif_")
)

Arguments

data

A data frame or tibble containing Comex data.

x

A character vector specifying the column name prefixes to match and sum. Defaults to c('qt_stat', 'kg_net', 'fob_', 'freight_', 'insurance_', 'cif_'), which captures common columns related to quantities, weights, and various costs.

Value

A tibble with one row containing the sum of each matched column. The column names in the result are the same as the input column names.

Details

This function simplifies the process of aggregating multiple related variables in Comex data by allowing you to specify prefixes instead of listing each column individually. For example, the default prefix 'fob_' would match columns like fob_usd, fob_brl, etc., and sum them all together.

Examples

# Create a sample Comex dataset
comex_data <- data.frame(
  qt_stat = c(100, 250, 80),
  kg_net = c(5000, 12000, 3500),
  fob_usd = c(15000, 38000, 10000),
  fob_brl = c(75000, 190000, 50000),
  freight_usd = c(2000, 5000, 1500)  # Additional column with 'fob_' prefix
)

# Aggregate columns starting with default prefixes
summary_data <- comex_sum(comex_data)
summary_data
#>   qt_stat kg_net fob_usd fob_brl freight_usd
#> 1     430  20500   63000  315000        8500

# Aggregate only columns starting with 'qt_' and 'fob_'
summary_data <- comex_sum(comex_data, x = c('qt_', 'fob_'))
summary_data
#>   qt_stat fob_usd fob_brl
#> 1     430   63000  315000