A sophisticated data transformation tool for generating column pair combinations and creating nested data structures with advanced configuration options.
c2p_nest(data, cols2bind, by = NULL, pairs_n = 2, sep = "-", nest_type = "dt")
Input data frame
or data table
Must contain valid columns for transformation
Supports multiple data types
Column specification for pair generation
Can be a character
vector of column names
Can be a numeric
vector of column indices
Must reference existing columns in the dataset
Optional grouping specification
Can be a character
vector of column names
Can be a numeric
vector of column indices
Enables hierarchical nested transformations
Supports multi-level aggregation
Default is NULL
numeric
indicating combination size
Minimum value: 2
Maximum value: Length of cols2bind
Controls column pair complexity
Default is 2
character
separator for pair naming
Used in generating combination identifiers
Must be a single character
Default is "-"
Output nesting format
"dt"
: Returns nested data table
(default)
"df"
: Returns nested data frame
data table
containing nested transformation results
Includes pairs
column identifying column combinations
Contains data
column storing nested data structures
Supports optional grouping variables
Advanced Transformation Mechanism:
Input validation and preprocessing
Dynamic column combination generation
Flexible pair transformation
Nested data structure creation
Transformation Process:
Validate input parameters and column specifications
Convert numeric indices to column names if necessary
Generate column combinations
Create subset data tables
Merge and nest transformed data
Column Specification:
Supports both column names and numeric indices
Numeric indices must be within valid range (1 to ncol)
Column names must exist in the dataset
Flexible specification for both cols2bind and by parameters
Key Operation Constraints:
Requires non-empty input data
Column specifications must be valid (either names or indices)
Supports flexible combination strategies
Computational complexity increases with combination size
utils::combn()
Combination generation
# Example data preparation: Define column names for combination
col_names <- c("Sepal.Length", "Sepal.Width", "Petal.Length")
# Example 1: Basic column-to-pairs nesting with custom separator
c2p_nest(
iris, # Input iris dataset
cols2bind = col_names, # Columns to be combined as pairs
pairs_n = 2, # Create pairs of 2 columns
sep = "&" # Custom separator for pair names
)
#> pairs data
#> <char> <list>
#> 1: Sepal.Length&Sepal.Width <data.table[150x4]>
#> 2: Sepal.Length&Petal.Length <data.table[150x4]>
#> 3: Sepal.Width&Petal.Length <data.table[150x4]>
# Returns a nested data.table where:
# - pairs: combined column names (e.g., "Sepal.Length&Sepal.Width")
# - data: list column containing data.tables with value1, value2 columns
# Example 2: Column-to-pairs nesting with numeric indices and grouping
c2p_nest(
iris, # Input iris dataset
cols2bind = 1:3, # First 3 columns to be combined
pairs_n = 2, # Create pairs of 2 columns
by = 5 # Group by 5th column (Species)
)
#> pairs Species data
#> <char> <fctr> <list>
#> 1: Sepal.Length-Sepal.Width setosa <data.table[50x3]>
#> 2: Sepal.Length-Sepal.Width versicolor <data.table[50x3]>
#> 3: Sepal.Length-Sepal.Width virginica <data.table[50x3]>
#> 4: Sepal.Length-Petal.Length setosa <data.table[50x3]>
#> 5: Sepal.Length-Petal.Length versicolor <data.table[50x3]>
#> 6: Sepal.Length-Petal.Length virginica <data.table[50x3]>
#> 7: Sepal.Width-Petal.Length setosa <data.table[50x3]>
#> 8: Sepal.Width-Petal.Length versicolor <data.table[50x3]>
#> 9: Sepal.Width-Petal.Length virginica <data.table[50x3]>
# Returns a nested data.table where:
# - pairs: combined column names
# - Species: grouping variable
# - data: list column containing data.tables grouped by Species