Movatterモバイル変換


[0]ホーム

URL:


Data Formats for andorR

TheandorR package is designed to be flexible, allowingyou to define your decision tree structure in several different formatsdepending on your preference and source data. This guide explains thethree primary formats and the functions used to load them.

The examples below use theethical investment decisiontree, which is included as a dataset within this package.

1. The Hierarchical Format (YAML or JSON)

This is often the most intuitive and human-readable format, as itsindented structure visually represents the tree’s hierarchy. It ishighly recommended for creating and managing trees in a text editor.

Required Fields:

YAML format

An excerpt from a YAML file (ethical.yml) looks likethis:

name: Invest in Company Xrule: ANDnodes:-name: Financial Viabilityrule: ANDnodes:-name: Profitability and Growth Signalsrule: ORquestion:"Is the company showing strong signs of profitability?"nodes:-name: FIN1question:"Company demonstrates consistent, high revenue growth."# ... and so on

To load a tree from a YAML file, use theload_tree_yaml() function.

# This code shows how to load a YAML file from the package's example datayaml_path<-system.file("extdata","ethical.yml",package ="andorR")tree_from_yaml<-load_tree_yaml(yaml_path)# View the loaded treeprint(tree_from_yaml)

JSON format

An excerpt from a JSON file (ethical.json) looks likethis:

{"name":"Invest in Company X","rule":"AND","nodes":[{"name":"Financial Viability","rule":"AND","nodes":[{"name":"Profitability and Growth Signals","rule":"OR","question":"Is the company showing strong signs of profitability?","nodes":[{"name":"FIN1","question":"Company demonstrates consistent, high revenue growth."},#...andsoon

To load a tree from a JSON file, use theload_tree_json() function.

# This code shows how to load a JSON file from the package's example datajson_path<-system.file("extdata","ethical.json",package ="andorR")tree_from_json<-load_tree_json(json_path)# View the loaded treeprint(tree_from_json)

2. The Relational (ID/Parent) Format

This is a flat, tabular format common in spreadsheets and databases.The tree structure is defined by aparent column thatcontains theid of its parent node. This is also the formatof theethical dataset included in the package.

Required Fields:

The first few rows of theethical dataset look likethis:

Example of the Relational (ID/Parent) Format
idnamequestionruleparent
0Invest in Company XNAANDNA
1Financial ViabilityNAAND0
2Acceptable Environmental StewardshipNAOR0
3Demonstrable Social ResponsibilityNAOR0
4Strong Corporate GovernanceNAAND0
5Profitability and Growth SignalsNAOR1
6Solvency and StabilityNAAND1
7FIN1Company demonstrates consistent, high revenuegrowth.NA5
8FIN2Company maintains a high net profit margin for itsindustry.NA5
9FIN3Company holds a dominant or rapidly growing marketshare.NA5

To load this format, you can useload_tree_df() if youalready have a data frame, orload_tree_csv() to read itfrom a.csv file.

# Load the example 'ethical' data frame that comes with the packagedata(ethical)# Build the tree from the data frame objecttree_from_df<-load_tree_df(ethical)# The structure is identical to the one loaded from YAMLprint(tree_from_df$children[[1]]$name)# Prints "Financial Viability"#> [1] "Financial Viability"

3. The Path String Format

This is another flat-file format that is very robust. The hierarchyis defined by a singlepath column that contains the full,delimited path from the root to each node.

Required Fields:

An excerpt from a CSV file in this format would look like this:

Example of the Path String Format
pathrule
Invest in Company XAND
Invest in Company X/Financial ViabilityAND
Invest in Company X/Financial Viability/Profitabilityand Growth SignalsOR
Invest in Company X/Financial Viability/Profitabilityand Growth Signals/FIN1NA

To load this format from a CSV file, use theload_tree_csv_path() function.

# This code shows how to load a path string CSV from the package's example datapath_csv_path<-system.file("extdata","ethical_path.csv",package ="andorR")tree_from_path<-load_tree_csv_path(path_csv_path)print(tree_from_path)

Summary of Formats

Diagram showing relationships between different formats

Format NameBest ForKey StructureCore Loading Function
HierarchicalManual creation, readability, version control(git)Indented YAML or JSON file with anodeskeyload_tree_yaml() orload_tree_json()
RelationalExporting from databases, spreadsheets (Excel)A flat table withid andparent columnsload_tree_csv()
Path StringProgrammatic generation, ensuring structuralintegrityA flat table with a singlepathcolumnload_tree_csv_path()

[8]ページ先頭

©2009-2025 Movatter.jp