A sophisticated data transformation tool for performing row pair conversion and creating nested data structures. It smartly iterates through variables to perfectly preserve non-target contextual variables while utilizing native dcast for extreme performance.

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

Arguments

data

Input data frame or data table.

rows2bind

A character column name or numeric index to be used as row values.

by

A character vector or numeric vector of column indices to transform.

nest_type

Output nesting format ("dt" or "df"). Default "dt".

Value

A nested data.table containing name and data columns, with all contextual features preserved inside the nested structures.

Examples

# Example: Row-to-pairs nesting with column names
r2p_nest(
  mtcars,
  rows2bind = "cyl",
  by = c("hp", "drat", "wt")
)
#>      name                data
#>    <char>              <list>
#> 1:     hp <data.table[32x12]>
#> 2:   drat <data.table[32x12]>
#> 3:     wt <data.table[32x12]>
# 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
#>    <char>              <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
#>    <char>              <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