Converts values in a dataset to different units based on predefined transformations or custom scaling. Supports manual and automatic conversions for economic and trade-related metrics.
Usage
convert_units(
data,
change_unit_from = NULL,
change_unit_to = NULL,
adjustment = NULL,
value_col = "Value",
unit_col = "Unit",
variable_select = NULL,
variable_col = "Variable",
scale_auto = NULL
)
Arguments
- data
A data structure (list, data frame, or nested combination).
- change_unit_from
Character vector. Units to be converted (case-insensitive).
- change_unit_to
Character vector. Target units corresponding to `change_unit_from`.
- adjustment
Character or numeric vector. Specifies conversion operations (e.g., `"/1000"` to convert million to billion).
- value_col
Character. Column name containing values to adjust (default: `"Value"`).
- unit_col
Character. Column name containing unit information (default: `"Unit"`).
- variable_select
Optional character vector. If provided, only these variables are converted.
- variable_col
Character. Column name containing variable identifiers (default: `"Variable"`).
- scale_auto
Optional character vector of predefined conversion rules: - `"mil2bil"`: Converts million USD to billion USD (divides by 1000). - `"bil2mil"`: Converts billion USD to million USD (multiplies by 1000). - `"pct2frac"`: Converts percent to fraction (divides by 100). - `"frac2pct"`: Converts fraction to percent (multiplies by 100).
Details
If both `change_unit_from` and `scale_auto` are provided, the function prompts the user to choose between manual and automatic conversion.
Examples
# Load Sample Data:
sl4_data1 <- HARplus::load_sl4x(system.file("extdata/in", "EXP1.sl4",
package = "GTAPViz"))
# Get Data by Variable Name
sl4_data1 <- HARplus::get_data_by_var(c("qgdp", "EV"),sl4_data1)
# Convert million USD to billion USD
gtap_data <- convert_units(sl4_data1,
change_unit_from = "million USD",
change_unit_to = "billion USD",
adjustment = "/1000"
)
# Automatic conversion from percent to fraction
gtap_data <- convert_units(sl4_data1, scale_auto = "pct2frac")