Learn R Programming

mintyr (version 0.1.0)

convert_nest: Convert Nested Columns Between data.frame and data.table

Description

The convert_nest function transforms a data.frame or data.table by converting nested columns to either data.frame or data.table format while preserving the original data structure.

Usage

convert_nest(data, to = c("df", "dt"), nest_cols = NULL)

Value

A transformed data.frame or data.table with nested columns converted to the specified format.

Arguments

data

A data.frame or data.table containing nested columns

to

A character string specifying the target format. Options are "df" (data frame) or "dt" (data table). Defaults to "df".

nest_cols

A character vector of column names containing nested data. If NULL, the function automatically detects list columns.

Details

Advanced Nested Column Conversion Features:

  • Intelligent automatic detection of nested columns

  • Comprehensive conversion of entire data structure

  • Selective conversion of specified nested columns

  • Non-destructive transformation with data copying

Input Validation and Error Handling:

  • Validates existence of specified nested columns

  • Verifies that specified columns are actually list columns

  • Provides informative error messages for invalid inputs

  • Ensures data integrity through comprehensive checks

Conversion Strategies:

  1. Nested column identification based on is.list() detection

  2. Preservation of original data integrity

  3. Flexible handling of mixed data structures

  4. Consistent type conversion across nested elements

Nested Column Handling:

  • Supports conversion of list columns

  • Handles data.table, data.frame, and generic list inputs

  • Maintains original column structure and order

  • Prevents in-place modification of source data

Examples

Run this code
# Example 1: Create nested data structures
# Create single nested column
df_nest1 <- iris |> 
  dplyr::group_nest(Species)     # Group and nest by Species

# Create multiple nested columns
df_nest2 <- iris |>
  dplyr::group_nest(Species) |>  # Group and nest by Species
  dplyr::mutate(
    data2 = purrr::map(          # Create second nested column
      data,
      dplyr::mutate, 
      c = 2
    )
  )

# Example 2: Convert nested structures
# Convert data frame to data table
convert_nest(
  df_nest1,                      # Input nested data frame
  to = "dt"                      # Convert to data.table
)

# Convert specific nested columns
convert_nest(
  df_nest2,                      # Input nested data frame
  to = "dt",                     # Convert to data.table
  nest_cols = "data"             # Only convert 'data' column
)

# Example 3: Convert data table to data frame
dt_nest <- mintyr::w2l_nest(
  data = iris,                   # Input dataset
  cols2l = 1:2                   # Columns to nest
)
convert_nest(
  dt_nest,                       # Input nested data table
  to = "df"                      # Convert to data frame
)

Run the code above in your browser using DataLab