Title: | Enhanced R Package for 'GEMPACK' .har and .sl4 Files |
---|---|
Description: | Provides tools for processing and analyzing .har and .sl4 files, making it easier for 'GEMPACK' users and 'GTAP' researchers to handle large economic datasets. It simplifies the management of multiple experiment results, enabling faster and more efficient comparisons without complexity. Users can extract, restructure, and merge data seamlessly, ensuring compatibility across different tools. The processed data can be exported and used in 'R', 'Stata', 'Python', 'Julia', or any software that supports Text, CSV, or 'Excel' formats. |
Authors: | Pattawee Puangchit [aut, cre] |
Maintainer: | Pattawee Puangchit <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.1 |
Built: | 2025-03-16 08:20:47 UTC |
Source: | https://github.com/bodysbobb/harplus |
Compares variable structures across multiple SL4 and HAR datasets to ensure compatibility. Identifies matching and mismatched variable structures, helping users diagnose inconsistencies.
compare_var_structure(variables = NULL, ..., keep_unique = FALSE)
compare_var_structure(variables = NULL, ..., keep_unique = FALSE)
variables |
Character vector. Variable names to compare. Use |
... |
Named SL4 or HAR objects to compare. |
keep_unique |
Logical. If |
Verifies whether variables have consistent structures across multiple datasets.
Ensures correct ordering of dimensions and checks for structural compatibility.
If keep_unique = TRUE
, returns a list of unique variable structures instead of performing a direct comparison.
Useful for merging or aligning datasets before further processing.
Helps detect differences in variable dimensions, which may arise due to model updates or dataset variations.
A list containing:
match
: A data frame listing variables with identical structures across datasets.
diff
: A data frame listing variables with mismatched structures, useful for debugging and alignment.
If keep_unique = TRUE
, instead of match
and diff
, returns a data frame with distinct variable structures across datasets.
Pattawee Puangchit
get_var_structure
, get_dim_patterns
, get_dim_elements
# Import sample data: har_data1 <- load_harx(system.file("extdata", "TAR10-WEL.har", package = "HARplus")) har_data2 <- load_harx(system.file("extdata", "SUBT10-WEL.har", package = "HARplus")) # Compare structure for a single variable across multiple datasets compare_var_structure("A", har_data1, har_data2) # Compare structure for multiple variables across multiple datasets comparison_multiple <- compare_var_structure(c("A", "E1"), har_data1, har_data2) # Extract unique variable structures across multiple datasets unique_vars <- compare_var_structure("ALL", har_data1, har_data2, keep_unique = TRUE)
# Import sample data: har_data1 <- load_harx(system.file("extdata", "TAR10-WEL.har", package = "HARplus")) har_data2 <- load_harx(system.file("extdata", "SUBT10-WEL.har", package = "HARplus")) # Compare structure for a single variable across multiple datasets compare_var_structure("A", har_data1, har_data2) # Compare structure for multiple variables across multiple datasets comparison_multiple <- compare_var_structure(c("A", "E1"), har_data1, har_data2) # Extract unique variable structures across multiple datasets unique_vars <- compare_var_structure("ALL", har_data1, har_data2, keep_unique = TRUE)
Exports structured SL4 or HAR data to multiple file formats, including CSV, Stata, TXT, RDS, and XLSX. Supports nested lists, automatic subfolder creation, and multi-sheet Excel exports.
export_data( data, output_path, format = "csv", prefix = "", create_subfolder = FALSE, multi_sheet_xlsx = FALSE, xlsx_filename = NULL, report_output = FALSE )
export_data( data, output_path, format = "csv", prefix = "", create_subfolder = FALSE, multi_sheet_xlsx = FALSE, xlsx_filename = NULL, report_output = FALSE )
data |
A list or data frame. The SL4 or HAR data to export. |
output_path |
Character. The base output directory or file path. |
format |
Character. The export format ( |
prefix |
Character. An optional prefix added to exported file names. Default is |
create_subfolder |
Logical. If |
multi_sheet_xlsx |
Logical. If TRUE, exports lists as multi-sheet XLSX files. |
xlsx_filename |
An optional filename for the XLSX file (used when |
report_output |
Logical. If TRUE, generates an export report. |
Supports exporting data in "csv"
, "stata"
, "txt"
, "rds"
, and "xlsx"
formats.
Handles nested lists and exports each data frame individually.
Optionally creates subfolders for each format (create_subfolder = TRUE
).
Customizes file names using prefix
.
When multi_sheet_xlsx = TRUE, all exported data is stored in a single Excel workbook, with each dataset as a separate sheet.
If exporting to Stata ("stata"
format), column names containing .
will be replaced with _
to ensure compatibility.
If multi_sheet_xlsx = TRUE
, list elements are exported as separate sheets in a single XLSX file.
The function creates necessary directories if they do not exist.
A list containing the file paths of the exported data.
Pattawee Puangchit
pivot_data
, get_data_by_var
, get_data_by_dims
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract data data_multiple <- get_data_by_var(c("qo", "pca"), sl4_data) # Export export_data(data_multiple, file.path(tempdir(), "output_directory"), format = c("csv", "xlsx", "stata", "txt", "rds"), create_subfolder = TRUE, multi_sheet_xlsx = TRUE)
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract data data_multiple <- get_data_by_var(c("qo", "pca"), sl4_data) # Export export_data(data_multiple, file.path(tempdir(), "output_directory"), format = c("csv", "xlsx", "stata", "txt", "rds"), create_subfolder = TRUE, multi_sheet_xlsx = TRUE)
Retrieves structured data from SL4 or HAR objects based on specified dimension patterns. Supports multiple experiments and merging datasets while maintaining structured dimension metadata.
get_data_by_dims( patterns = NULL, ..., experiment_names = NULL, subtotal_level = FALSE, rename_cols = NULL, merge_data = FALSE, pattern_mix = FALSE )
get_data_by_dims( patterns = NULL, ..., experiment_names = NULL, subtotal_level = FALSE, rename_cols = NULL, merge_data = FALSE, pattern_mix = FALSE )
patterns |
Character vector. Dimension patterns to extract. Use |
... |
One or more SL4 or HAR data objects loaded using |
experiment_names |
Character vector. Names assigned to each dataset. If |
subtotal_level |
Character or logical. Determines which decomposition levels to retain:
|
rename_cols |
Named vector. Column name replacements ( |
merge_data |
Logical. If |
pattern_mix |
Logical. If |
Extracts variables matching specified dimension patterns.
Allows for flexible pattern matching (pattern_mix = TRUE
).
Supports merging data across multiple experiments (merge_data = TRUE
).
Provides column renaming functionality (rename_cols
).
Handles subtotal filtering (subtotal_level
), controlling whether "TOTAL"
or decomposed values are retained.
A structured list of extracted data:
If merge_data = FALSE
, returns a named list where each element corresponds to an experiment.
If merge_data = TRUE
, returns a named list of all merged data
Pattawee Puangchit
get_data_by_var
, group_data_by_dims
# Import sample data: sl4_data <- load_sl4x( system.file("extdata", "TAR10.sl4", package = "HARplus") ) sl4_data1 <- load_sl4x( system.file("extdata", "SUBT10.sl4", package = "HARplus") ) # Extract data for a single dimension pattern data_single_pattern <- get_data_by_dims( "comm*reg", sl4_data ) # Extract multiple dimension patterns data_multiple_patterns <- get_data_by_dims( c("comm*reg", "REG*ACTS"), sl4_data ) # Extract all dimension patterns separately from multiple datasets data_all_patterns <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = FALSE ) # Merge data for identical patterns across multiple datasets data_merged_patterns <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = TRUE ) # Merge data while allowing interchangeable dimensions (e.g., A*B = B*A) data_pattern_mixed <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = TRUE, pattern_mix = TRUE ) # Retain only "TOTAL" values data_total_only <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "total" ) data_total_only_alt <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = FALSE ) # Retain only decomposed components data_decomposed_only <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "decomposed" ) # Retain all value levels data_all_decomp <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "all" ) data_all_decomp_alt <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = TRUE ) # Rename specific columns data_renamed <- get_data_by_dims( "comm*reg", sl4_data, rename_cols = c(REG = "Region", COMM = "Commodity") ) # Merge data with custom experiment names data_merged_experiments <- get_data_by_dims( "comm*reg", sl4_data, sl4_data1, experiment_names = c("EXP1", "EXP2"), merge_data = TRUE )
# Import sample data: sl4_data <- load_sl4x( system.file("extdata", "TAR10.sl4", package = "HARplus") ) sl4_data1 <- load_sl4x( system.file("extdata", "SUBT10.sl4", package = "HARplus") ) # Extract data for a single dimension pattern data_single_pattern <- get_data_by_dims( "comm*reg", sl4_data ) # Extract multiple dimension patterns data_multiple_patterns <- get_data_by_dims( c("comm*reg", "REG*ACTS"), sl4_data ) # Extract all dimension patterns separately from multiple datasets data_all_patterns <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = FALSE ) # Merge data for identical patterns across multiple datasets data_merged_patterns <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = TRUE ) # Merge data while allowing interchangeable dimensions (e.g., A*B = B*A) data_pattern_mixed <- get_data_by_dims( NULL, sl4_data, sl4_data1, merge_data = TRUE, pattern_mix = TRUE ) # Retain only "TOTAL" values data_total_only <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "total" ) data_total_only_alt <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = FALSE ) # Retain only decomposed components data_decomposed_only <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "decomposed" ) # Retain all value levels data_all_decomp <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = "all" ) data_all_decomp_alt <- get_data_by_dims( "comm*reg", sl4_data, subtotal_level = TRUE ) # Rename specific columns data_renamed <- get_data_by_dims( "comm*reg", sl4_data, rename_cols = c(REG = "Region", COMM = "Commodity") ) # Merge data with custom experiment names data_merged_experiments <- get_data_by_dims( "comm*reg", sl4_data, sl4_data1, experiment_names = c("EXP1", "EXP2"), merge_data = TRUE )
Extracts structured data for one or more variables from SL4 or HAR objects, transforming array-like data into a tidy format.
get_data_by_var( var_names = NULL, ..., experiment_names = NULL, subtotal_level = FALSE, rename_cols = NULL, merge_data = FALSE )
get_data_by_var( var_names = NULL, ..., experiment_names = NULL, subtotal_level = FALSE, rename_cols = NULL, merge_data = FALSE )
var_names |
Character vector. Variable names to extract. Use |
... |
One or more SL4 or HAR data objects loaded using |
experiment_names |
Character vector. Names assigned to each dataset. If |
subtotal_level |
Character or logical. Determines which decomposition levels to retain:
|
rename_cols |
Named vector. Column name replacements ( |
merge_data |
Logical. If |
Retrieves specific variables, multiple variables, or all available variables from SL4 or HAR datasets.
Supports merging data from multiple experiments (merge_data = TRUE
).
Allows renaming of column names (rename_cols
).
Handles subtotal filtering (subtotal_level
), controlling whether "TOTAL"
or decomposed values are retained.
A list of structured data:
If merge_data = FALSE
, returns a named list where each element corresponds to an experiment.
If merge_data = TRUE
, returns a named list of all merged data
Pattawee Puangchit
get_data_by_dims
, , group_data_by_dims
, load_sl4x
, load_harx
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data1 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract a single variable data_qo <- get_data_by_var("qo", sl4_data) # Extract multiple variables data_multiple <- get_data_by_var(c("qo", "qgdp"), sl4_data) # Extract all variables separately from multiple datasets data_all <- get_data_by_var(NULL, sl4_data, sl4_data1, merge_data = FALSE) # Merge variable data across multiple datasets data_merged <- get_data_by_var(NULL, sl4_data, sl4_data1, merge_data = TRUE) # Retain only "TOTAL" values, removing decomposed components (subtotal_level = "total" or FALSE) data_total_only <- get_data_by_var("qo", sl4_data, subtotal_level = "total") data_total_only_alt <- get_data_by_var("qo", sl4_data, subtotal_level = FALSE) # Retain only decomposed components, removing "TOTAL" (subtotal_level = "decomposed") data_decomposed_only <- get_data_by_var("qo", sl4_data, subtotal_level = "decomposed") # Retain all value levels (subtotal_level = "all" or TRUE) data_all_decomp <- get_data_by_var("qo", sl4_data, subtotal_level = "all") data_all_decomp_alt <- get_data_by_var("qo", sl4_data, subtotal_level = TRUE) # Rename specific columns data_renamed <- get_data_by_var("qo", sl4_data, rename_cols = c(REG = "Region", COMM = "Commodity")) # Merge data across multiple datasets with custom experiment names data_merged_experiments <- get_data_by_var("qo", sl4_data, sl4_data1, experiment_names = c("EXP1", "EXP2"), merge_data = TRUE)
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data1 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract a single variable data_qo <- get_data_by_var("qo", sl4_data) # Extract multiple variables data_multiple <- get_data_by_var(c("qo", "qgdp"), sl4_data) # Extract all variables separately from multiple datasets data_all <- get_data_by_var(NULL, sl4_data, sl4_data1, merge_data = FALSE) # Merge variable data across multiple datasets data_merged <- get_data_by_var(NULL, sl4_data, sl4_data1, merge_data = TRUE) # Retain only "TOTAL" values, removing decomposed components (subtotal_level = "total" or FALSE) data_total_only <- get_data_by_var("qo", sl4_data, subtotal_level = "total") data_total_only_alt <- get_data_by_var("qo", sl4_data, subtotal_level = FALSE) # Retain only decomposed components, removing "TOTAL" (subtotal_level = "decomposed") data_decomposed_only <- get_data_by_var("qo", sl4_data, subtotal_level = "decomposed") # Retain all value levels (subtotal_level = "all" or TRUE) data_all_decomp <- get_data_by_var("qo", sl4_data, subtotal_level = "all") data_all_decomp_alt <- get_data_by_var("qo", sl4_data, subtotal_level = TRUE) # Rename specific columns data_renamed <- get_data_by_var("qo", sl4_data, rename_cols = c(REG = "Region", COMM = "Commodity")) # Merge data across multiple datasets with custom experiment names data_merged_experiments <- get_data_by_var("qo", sl4_data, sl4_data1, experiment_names = c("EXP1", "EXP2"), merge_data = TRUE)
Extracts and lists unique dimension elements (e.g., REG
, COMM
, ACTS
) from one or more datasets.
get_dim_elements(..., keep_unique = FALSE)
get_dim_elements(..., keep_unique = FALSE)
... |
One or more structured SL4 or HAR objects containing dimension information. |
keep_unique |
Logical. If |
A data frame containing unique dimension elements.
Pattawee Puangchit
get_dim_patterns
, get_var_structure
# Import sample data: sl4_data1 <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract dimension elements from a single dataset get_dim_elements(sl4_data1) # Extract dimension elements from multiple datasets get_dim_elements(sl4_data1, sl4_data2) # Extract unique dimension elements across datasets get_dim_elements(sl4_data1, sl4_data2, keep_unique = TRUE)
# Import sample data: sl4_data1 <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract dimension elements from a single dataset get_dim_elements(sl4_data1) # Extract dimension elements from multiple datasets get_dim_elements(sl4_data1, sl4_data2) # Extract unique dimension elements across datasets get_dim_elements(sl4_data1, sl4_data2, keep_unique = TRUE)
Extracts and lists unique dimension patterns (e.g., REG*COMM
, REG*REG*ACTS
) from one or more datasets.
get_dim_patterns(..., keep_unique = FALSE)
get_dim_patterns(..., keep_unique = FALSE)
... |
One or more structured SL4 or HAR objects containing dimension information. |
keep_unique |
Logical. If |
Extracts dimension structure details from the dataset.
If multiple datasets are provided, combines their dimension information.
If keep_unique = TRUE
, returns only distinct dimension patterns.
A data frame containing:
DimPattern
: The unique dimension patterns.
Pattawee Puangchit
get_dim_elements
, get_var_structure
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract dimension patterns get_dim_patterns(sl4_data) # Extract only unique dimension patterns across datasets get_dim_patterns(sl4_data, sl4_data2, keep_unique = TRUE)
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Extract dimension patterns get_dim_patterns(sl4_data) # Extract only unique dimension patterns across datasets get_dim_patterns(sl4_data, sl4_data2, keep_unique = TRUE)
Generates a summary of the variables within one or more SL4 or HAR objects, listing their dimension sizes, structures, and optionally, column and observation counts.
get_var_structure(variables = NULL, ..., include_col_size = FALSE)
get_var_structure(variables = NULL, ..., include_col_size = FALSE)
variables |
Character vector. Variable names to summarize. Use |
... |
One or more SL4 or HAR objects created using |
include_col_size |
Logical. If |
Extracts dimension structures for variables in one or more SL4 or HAR datasets.
If include_col_size = TRUE
, adds column and observation counts.
Supports multiple datasets and returns results as a named list, with each dataset’s summary stored separately.
Can summarize specific variables or "ALL"
.
A named list, where each element contains a data frame with:
Variable
: The variable name.
Dimensions
: The associated dimensions.
DimSize
: The number of dimensions.
DataShape
: The shape of the data (e.g., 10x20x30
).
No.Col
: (Optional) The number of columns.
No.Obs
: (Optional) The number of observations.
Pattawee Puangchit
get_dim_patterns
, get_dim_elements
# Import data sample: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data1 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Get summary for all variables in a single dataset get_var_structure(data_obj = sl4_data) # Get summary for specific variables get_var_structure(c("gdp", "trade"), sl4_data) # Include column and observation counts get_var_structure("ALL", sl4_data, include_col_size = TRUE) # Compare structures across multiple datasets get_var_structure("ALL", sl4_data, sl4_data1) # Include column and observation counts across multiple datasets get_var_structure("ALL", sl4_data, sl4_data1, include_col_size = TRUE)
# Import data sample: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data1 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Get summary for all variables in a single dataset get_var_structure(data_obj = sl4_data) # Get summary for specific variables get_var_structure(c("gdp", "trade"), sl4_data) # Include column and observation counts get_var_structure("ALL", sl4_data, include_col_size = TRUE) # Compare structures across multiple datasets get_var_structure("ALL", sl4_data, sl4_data1) # Include column and observation counts across multiple datasets get_var_structure("ALL", sl4_data, sl4_data1, include_col_size = TRUE)
Groups extracted SL4 or HAR data based on specified dimension structures and priority rules. Supports automatic renaming, merging, subtotal filtering, and structured metadata handling.
group_data_by_dims( patterns = NULL, ..., priority, rename_cols = NULL, experiment_names = NULL, subtotal_level = FALSE, auto_rename = FALSE )
group_data_by_dims( patterns = NULL, ..., priority, rename_cols = NULL, experiment_names = NULL, subtotal_level = FALSE, auto_rename = FALSE )
patterns |
Character vector. Dimension patterns to extract. Use |
... |
One or more SL4 or HAR objects loaded using |
priority |
Named list. Specifies priority dimension elements ( |
rename_cols |
Named vector. Column name replacements ( |
experiment_names |
Character vector. Names assigned to each dataset. If |
subtotal_level |
Character or logical. Determines which decomposition levels to retain:
|
auto_rename |
Logical. If |
Groups extracted variables based on dimension elements.
Applies predefined priority rules to structure the data.
Allows automatic renaming of dimensions (auto_rename = TRUE
).
Supports merging of grouped data across multiple experiments.
Handles subtotal filtering (subtotal_level
), controlling whether "TOTAL"
or decomposed values are retained.
A structured list of grouped data:
A named list where each element corresponds to a dimension size group (e.g., "2D", "3D").
Each group contains dimension-grouped data based on priority rules.
If unmerged data exists, includes a report attribute detailing merge issues.
Pattawee Puangchit
get_data_by_dims
, get_data_by_var
, load_sl4x
, load_harx
# Import sample data sl4_data1 <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Case 1: Multiple priority levels (Sector then Region) with auto_rename priority_list <- list( "Sector" = c("COMM", "ACTS"), "Region" = c("REG") ) grouped_data_multiple <- group_data_by_dims( patterns = "ALL", sl4_data1, priority = priority_list, auto_rename = TRUE ) # Case 2: Single priority (Region only) with auto_rename priority_list <- list("Region" = c("REG")) grouped_data_single <- group_data_by_dims( patterns = "ALL", sl4_data1, sl4_data2, priority = priority_list, auto_rename = TRUE ) # Case 3: Multiple priorities without auto_rename priority_list <- list( "Sector" = c("COMM", "ACTS"), "Region" = c("REG") ) grouped_data_no_rename <- group_data_by_dims( patterns = "ALL", sl4_data1, priority = priority_list, auto_rename = FALSE )
# Import sample data sl4_data1 <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) sl4_data2 <- load_sl4x(system.file("extdata", "SUBT10.sl4", package = "HARplus")) # Case 1: Multiple priority levels (Sector then Region) with auto_rename priority_list <- list( "Sector" = c("COMM", "ACTS"), "Region" = c("REG") ) grouped_data_multiple <- group_data_by_dims( patterns = "ALL", sl4_data1, priority = priority_list, auto_rename = TRUE ) # Case 2: Single priority (Region only) with auto_rename priority_list <- list("Region" = c("REG")) grouped_data_single <- group_data_by_dims( patterns = "ALL", sl4_data1, sl4_data2, priority = priority_list, auto_rename = TRUE ) # Case 3: Multiple priorities without auto_rename priority_list <- list( "Sector" = c("COMM", "ACTS"), "Region" = c("REG") ) grouped_data_no_rename <- group_data_by_dims( patterns = "ALL", sl4_data1, priority = priority_list, auto_rename = FALSE )
Reads a GEMPACK HAR file and extracts structured data while maintaining compatibility with standard HAR formats. Provides flexibility in naming conventions and header selection.
load_harx( file_path, coefAsname = FALSE, lowercase = FALSE, select_header = NULL )
load_harx( file_path, coefAsname = FALSE, lowercase = FALSE, select_header = NULL )
file_path |
Character. The file path to the HAR file. |
coefAsname |
Logical. If |
lowercase |
Logical. If |
select_header |
Character vector. Specific headers to read; if |
Uses load_harplus()
internally for efficient HAR file reading.
Allows optional conversion of variable names to lowercase (lowercase = TRUE
).
Supports coefficient-based naming (coefAsname = TRUE
).
Enables selective header extraction via select_header = c("A", "E1")
.
Returns structured data with explicit dimension names and sizes.
A structured list containing:
data
: Extracted HAR variable data stored as matrices, arrays, or vectors.
dimension_info
: A list with:
dimension_string
: A textual representation of dimensions (e.g., "REGCOMMYEAR").
dimension_names
: The names of each dimension.
dimension_sizes
: The size of each dimension.
Pattawee Puangchit
load_sl4x
, get_data_by_var
, get_data_by_dims
# Path to example files har_path <- system.file("extdata", "TAR10-WEL.har", package = "HARplus") # Basic loading har_data <- load_harx(har_path) # Load with coefficient names har_data_coef <- load_harx(har_path, coefAsname = TRUE) # Load with lowercase names har_data_lower <- load_harx(har_path, lowercase = TRUE) # Load specific headers har_selected <- load_harx(har_path, select_header = c("A", "E1")) # Load with multiple options har_combined <- load_harx(har_path, coefAsname = TRUE, lowercase = TRUE, select_header = c("A", "E1"))
# Path to example files har_path <- system.file("extdata", "TAR10-WEL.har", package = "HARplus") # Basic loading har_data <- load_harx(har_path) # Load with coefficient names har_data_coef <- load_harx(har_path, coefAsname = TRUE) # Load with lowercase names har_data_lower <- load_harx(har_path, lowercase = TRUE) # Load specific headers har_selected <- load_harx(har_path, select_header = c("A", "E1")) # Load with multiple options har_combined <- load_harx(har_path, coefAsname = TRUE, lowercase = TRUE, select_header = c("A", "E1"))
Reads an SL4 file and processes its structured data into an enhanced SL4 object. Extracts structured variable information, dimensions, and handles subtotal columns.
load_sl4x(file_path, lowercase = FALSE, select_header = NULL)
load_sl4x(file_path, lowercase = FALSE, select_header = NULL)
file_path |
Character. The full path to the SL4 file to be read. |
lowercase |
Logical. If |
select_header |
Character vector. Specific headers to extract; if |
Uses load_harplus()
internally for optimized SL4 file reading.
Extracts variable names, dimension structures, and metadata.
Converts variable names to lowercase if lowercase = TRUE
.
Allows the selection of specific headers using select_header
.
Returns structured data with explicit dimension names and sizes.
A structured list containing:
data
: Extracted SL4 variable data, stored as arrays or matrices.
dimension_info
: A list with:
dimension_string
: A textual representation of dimensions (e.g., "REGCOMMYEAR").
dimension_names
: The names of each dimension.
dimension_sizes
: The size of each dimension.
Pattawee Puangchit
load_harx
, get_data_by_var
, get_data_by_dims
# Path to example files sl4_path <- system.file("extdata", "TAR10.sl4", package = "HARplus") # Basic loading sl4_data <- load_sl4x(sl4_path) # Load with lowercase names sl4_data_lower <- load_sl4x(sl4_path, lowercase = TRUE) # Load specific headers sl4_selected <- load_sl4x(sl4_path, select_header = c("qo", "qgdp"))
# Path to example files sl4_path <- system.file("extdata", "TAR10.sl4", package = "HARplus") # Basic loading sl4_data <- load_sl4x(sl4_path) # Load with lowercase names sl4_data_lower <- load_sl4x(sl4_path, lowercase = TRUE) # Load specific headers sl4_selected <- load_sl4x(sl4_path, select_header = c("qo", "qgdp"))
Transforms long-format SL4 or HAR data into wide format by pivoting selected columns. Supports both single data frames and nested lists.
pivot_data(data_obj, pivot_cols, name_repair = "unique")
pivot_data(data_obj, pivot_cols, name_repair = "unique")
data_obj |
A list or data frame. The SL4 or HAR data to pivot. |
pivot_cols |
Character vector. Column names to use as pivot keys. |
name_repair |
Character. Method for handling duplicate column names ( |
Uses tidyr::pivot_wider()
internally to reshape data.
Allows multiple columns to be pivoted simultaneously.
Recursively processes nested lists, ensuring all data frames are transformed.
A transformed data object where the specified pivot_cols
are pivoted into wide format.
Pattawee Puangchit
get_data_by_var
, get_data_by_dims
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract multiple variables data_multiple <- get_data_by_var(c("qo", "qxs"), sl4_data) # Pivot a single column pivoted_data <- pivot_data(data_multiple, pivot_cols = "REG") # Pivot multiple columns pivoted_data_multi <- pivot_data(data_multiple, pivot_cols = c("REG", "COMM"))
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract multiple variables data_multiple <- get_data_by_var(c("qo", "qxs"), sl4_data) # Pivot a single column pivoted_data <- pivot_data(data_multiple, pivot_cols = "REG") # Pivot multiple columns pivoted_data_multi <- pivot_data(data_multiple, pivot_cols = c("REG", "COMM"))
Creates hierarchical pivot tables from structured SL4 or HAR data, with optional Excel export. Supports both single data frames and nested lists, preserving dimension hierarchies.
pivot_data_hierarchy( data_obj, pivot_cols, name_repair = "unique", export = FALSE, file_path = NULL, xlsx_filename = NULL )
pivot_data_hierarchy( data_obj, pivot_cols, name_repair = "unique", export = FALSE, file_path = NULL, xlsx_filename = NULL )
data_obj |
A list or data frame. The SL4 or HAR data to pivot. |
pivot_cols |
Character vector. Column names to use as pivot keys in order of hierarchy. |
name_repair |
Character. Method for handling duplicate column names
( |
export |
Logical. If |
file_path |
Character. Required if export = TRUE. The path for Excel export. |
xlsx_filename |
Character. The name for the Excel file when using multi_sheet_xlsx. If NULL, uses the name of the dataset. Default is |
Transforms data into hierarchical pivot format with nested column headers.
Supports multiple pivot columns in specified order (e.g., REG > COMM).
Handles both single data frames and nested list structures.
Optional direct export to Excel with formatted hierarchical headers.
Uses efficient data processing with tidyr and openxlsx.
A pivoted data object with hierarchical structure:
If input is a data frame: Returns a hierarchical pivot table.
If input is a list: Returns a nested list of hierarchical pivot tables.
If export = TRUE: Invisibly returns the pivoted object after Excel export.
Pattawee Puangchit
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract data data_multiple <- get_data_by_var(c("qo", "pca"), sl4_data) # Create hierarchical pivot without export pivot_hier <- pivot_data_hierarchy(data_multiple, pivot_cols = c("REG", "COMM")) # Create and export to Excel in one step pivot_export <- pivot_data_hierarchy(data_multiple, pivot_cols = c("REG", "COMM"), export = TRUE, file_path = file.path(tempdir(), "pivot_output.xlsx"))
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Extract data data_multiple <- get_data_by_var(c("qo", "pca"), sl4_data) # Create hierarchical pivot without export pivot_hier <- pivot_data_hierarchy(data_multiple, pivot_cols = c("REG", "COMM")) # Create and export to Excel in one step pivot_export <- pivot_data_hierarchy(data_multiple, pivot_cols = c("REG", "COMM"), export = TRUE, file_path = file.path(tempdir(), "pivot_output.xlsx"))
Renames dimension and list names in structured SL4 or HAR objects.
rename_dims(data_obj, mapping_df, rename_list_names = FALSE)
rename_dims(data_obj, mapping_df, rename_list_names = FALSE)
data_obj |
A structured SL4 or HAR object. |
mapping_df |
A two-column data frame where the first column ( |
rename_list_names |
Logical. If |
Replaces old dimension names with new ones as specified in mapping_df
.
If rename_list_names = TRUE
, renames list element names as well.
Ensures consistency across SL4 and HAR datasets.
The modified SL4 or HAR object with updated dimension names and, optionally, updated list names.
Pattawee Puangchit
get_data_by_var
, get_data_by_dims
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Define a renaming map mapping_df <- data.frame( old = c("REG", "COMM"), new = c("Region", "Commodity") ) # Rename columns in the dataset rename_dims(sl4_data, mapping_df) # Rename both columns and list names rename_dims(sl4_data, mapping_df, rename_list_names = TRUE)
# Import sample data: sl4_data <- load_sl4x(system.file("extdata", "TAR10.sl4", package = "HARplus")) # Define a renaming map mapping_df <- data.frame( old = c("REG", "COMM"), new = c("Region", "Commodity") ) # Rename columns in the dataset rename_dims(sl4_data, mapping_df) # Rename both columns and list names rename_dims(sl4_data, mapping_df, rename_list_names = TRUE)