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.
convert_nest(data, to = c("df", "dt"))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.
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:
Nested column identification based on is.list() detection
Preservation of original data integrity through copying
Flexible handling of mixed data structures
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
Conversion Characteristics:
Non-destructive transformation of all nested columns
Automatic detection eliminates need for manual column specification
Supports flexible input and output formats
Minimal performance overhead
Warning Conditions:
Issues warning if no list columns are found in the input data
Returns original data unchanged when no conversion is needed
Provides clear messages for troubleshooting
# 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
)
#> Species data
#> <fctr> <list>
#> 1: setosa <data.table[50x4]>
#> 2: versicolor <data.table[50x4]>
#> 3: virginica <data.table[50x4]>
# 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
)
#> # A tibble: 2 × 2
#> name data
#> <fct> <list>
#> 1 Sepal.Length <tibble [150 × 4]>
#> 2 Sepal.Width <tibble [150 × 4]>