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.
convert_nest(data, to = c("df", "dt"), nest_cols = NULL)
A data.frame
or data.table
containing nested columns
A character
string specifying the target format.
Options are "df"
(data frame) or "dt"
(data table). Defaults to "df"
.
A character
vector of column names containing nested data.
If NULL
, the function automatically detects list columns.
A transformed data.frame
or data.table
with nested columns converted to the specified format.
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:
Nested column identification based on is.list()
detection
Preservation of original data integrity
Flexible handling of mixed data structures
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
Conversion Characteristics:
Non-destructive transformation of nested columns
Supports flexible input and output formats
Intelligent type detection and conversion
Minimal performance overhead
Error Conditions:
Throws error if specified columns don't exist in the input data
Throws error if specified columns are not list columns
Provides clear error messages for troubleshooting
Validates input parameters before processing
# 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]>
# 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
)
#> Species data data2
#> <fctr> <list> <list>
#> 1: setosa <data.table[50x4]> <tbl_df[50x5]>
#> 2: versicolor <data.table[50x4]> <tbl_df[50x5]>
#> 3: virginica <data.table[50x4]> <tbl_df[50x5]>
# 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]>