A sophisticated data transformation tool for performing row pair conversion and creating nested data structures with advanced configuration options.
r2p_nest(data, rows2bind, by, nest_type = "dt")
Input data frame
or data table
Must contain valid columns for transformation
Supports multiple data types
Row binding specification
Can be a character
column name
Can be a numeric
column index
Must be a single column identifier
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
Output nesting format
"dt"
: Returns nested data table
(default)
"df"
: Returns nested data frame
data table
containing nested transformation results
Includes name
column identifying source columns
Contains data
column storing nested data structures
Advanced Transformation Mechanism:
Input validation and preprocessing
Dynamic column identification
Flexible row pairing across specified columns
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
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
data.table::melt()
Long format conversion
data.table::dcast()
Wide format conversion
base::rbind()
Row binding utility
c2p_nest()
Column to pair nested transformation
# 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
)
#> name data
#> <fctr> <list>
#> 1: hp <data.table[32x12]>
#> 2: drat <data.table[32x12]>
#> 3: wt <data.table[32x12]>
# 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
)
#> name data
#> <fctr> <list>
#> 1: hp <data.table[32x12]>
#> 2: drat <data.table[32x12]>
#> 3: wt <data.table[32x12]>
# 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