c2p_nest: Column to Pair Nested Transformation

View source: R/c2p_nest.R

c2p_nestR Documentation

Column to Pair Nested Transformation

Description

A sophisticated data transformation tool for generating column pair combinations and creating nested data structures with advanced configuration options.

Usage

c2p_nest(data, cols2bind, by = NULL, pairs_n = 2, sep = "-", nest_type = "dt")

Arguments

data

Input ⁠data frame⁠ or ⁠data table⁠

  • Must contain valid columns for transformation

  • Supports multiple data types

cols2bind

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

by

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

pairs_n

numeric indicating combination size

  • Minimum value: 2

  • Maximum value: Length of cols2bind

  • Controls column pair complexity

  • Default is 2

sep

character separator for pair naming

  • Used in generating combination identifiers

  • Must be a single character

  • Default is "-"

nest_type

Output nesting format

  • "dt": Returns nested ⁠data table⁠ (default)

  • "df": Returns nested ⁠data frame⁠

Details

Advanced Transformation Mechanism:

  1. Input validation and preprocessing

  2. Dynamic column combination generation

  3. Flexible pair transformation

  4. 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

Value

⁠data table⁠ containing nested transformation results

  • Includes pairs column identifying column combinations

  • Contains data column storing nested data structures

  • Supports optional grouping variables

Note

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

See Also

  • utils::combn() Combination generation

Examples

# 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
)
# 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)
)
# Returns a nested data.table where:
# - pairs: combined column names
# - Species: grouping variable
# - data: list column containing data.tables grouped by Species

mintyr documentation built on April 4, 2025, 2:56 a.m.