Learn R Programming

mintyr (version 0.1.2)

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. Nested columns are automatically detected based on list column identification.

Usage

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

Value

A transformed data.frame or data.table with all nested columns converted to the specified format. If no nested columns are found, returns the original data with a warning.

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

Details

Advanced Nested Column Conversion Features:

  • Intelligent automatic detection of all nested (list) columns

  • Comprehensive conversion of entire data structure

  • Non-destructive transformation with data copying

  • Seamless handling of mixed nested structures

Automatic Detection and Validation:

  • Automatically identifies all list columns in the dataset

  • Issues warning if no nested columns are detected

  • Returns original data unchanged when no list columns exist

  • Ensures data integrity through comprehensive checks

Conversion Strategies:

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

  2. Preservation of original data integrity through copying

  3. Flexible handling of mixed data structures

  4. Consistent type conversion across all nested elements

Nested Column Handling:

  • Automatically processes all 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
)

# 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