r2p_nest: Row to Pair Nested Transformation

View source: R/r2p_nest.R

r2p_nestR Documentation

Row to Pair Nested Transformation

Description

A sophisticated data transformation tool for performing row pair conversion and creating nested data structures with advanced configuration options.

Usage

r2p_nest(data, rows2bind, by, nest_type = "dt")

Arguments

data

Input ⁠data frame⁠ or ⁠data table⁠

  • Must contain valid columns for transformation

  • Supports multiple data types

rows2bind

Row binding specification

  • Can be a character column name

  • Can be a numeric column index

  • Must be a single column identifier

by

Grouping specification for nested pairing

  • Can be a character vector of column names

  • Can be a numeric vector of column indices

  • Must specify at least one column

  • Supports multi-column transformation

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 identification

  3. Flexible row pairing across specified columns

  4. Nested data structure generation

Transformation Process:

  • Validate input parameters and column specifications

  • Convert numeric indices to column names if necessary

  • Reshape data from wide to long format

  • Perform column-wise nested transformation

  • Generate final nested structure

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 rows2bind and by parameters

Value

⁠data table⁠ containing nested transformation results

  • Includes name column identifying source columns

  • Contains data column storing nested data structures

Note

Key Operation Constraints:

  • Requires non-empty input data

  • Column specifications must be valid (either names or indices)

  • By parameter must specify at least one column

  • Low computational overhead

See Also

  • data.table::melt() Long format conversion

  • data.table::dcast() Wide format conversion

  • base::rbind() Row binding utility

  • c2p_nest() Column to pair nested transformation

Examples

# Example 1: Row-to-pairs nesting with column names
r2p_nest(
  mtcars,                     # Input mtcars dataset
  rows2bind = "cyl",          # Column to be used as row values
  by = c("hp", "drat", "wt")  # Columns to be transformed into pairs
)
# Returns a nested data.table where:
# - name: variable names (hp, drat, wt)
# - data: list column containing data.tables with rows grouped by cyl values

# Example 2: Row-to-pairs nesting with numeric indices
r2p_nest(
  mtcars,                     # Input mtcars dataset
  rows2bind = 2,              # Use 2nd column (cyl) as row values
  by = 4:6                    # Use columns 4-6 (hp, drat, wt) for pairs
)
# Returns a nested data.table where:
# - name: variable names from columns 4-6
# - data: list column containing data.tables with rows grouped by cyl values

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