The w2l_nest function reshapes wide-format data into long-format and nests it by specified columns.
It handles both data.frame and data.table objects and provides options for grouping and nesting the data.
w2l_nest(data, cols2l = NULL, by = NULL, nest_type = "dt")data.frame or data.table
Input dataset in wide format
Automatically converted to data.table if necessary
numeric or character columns to transform
Specifies columns for wide-to-long conversion
Can be column indices or column names
Default is NULL
numeric or character grouping variables
Optional columns for additional data stratification
Can be column indices or column names
Used to create hierarchical nested structures
Default is NULL
character output data type
Defines nested data object type
Possible values:
"dt": nested data.table
"df": nested data.frame
Default is "dt"
data.table with nested data in long format, grouped by specified columns if provided. Each row contains a nested data.table or data.frame under the column data, depending on nest_type.
If by is NULL, returns a data.table nested by name.
If by is specified, returns a data.table nested by name and the grouping variables.
The function melts the specified wide columns into long format and nests the resulting data by the name
column and any additional grouping variables specified in by. The nested data can be in the form of
data.table or data.frame objects, controlled by the nest_type parameter.
Both cols2l and by parameters accept either column indices or column names, providing flexible ways
to specify the columns for transformation and grouping.
Both cols2l and by parameters can be specified using either numeric indices or character column names.
When using numeric indices, they must be valid column positions in the data (1 to ncol(data)).
When using character names, all specified columns must exist in the data.
The function converts data.frame to data.table if necessary.
The nest_type parameter controls whether nested data are data.table ("dt") or data.frame ("df") objects.
If nest_type is not "dt" or "df", the function will stop with an error.
Related functions and packages:
tidytable::nest_by() Nest data.tables by group
# Example: Wide to long format nesting demonstrations
# Example 1: Basic nesting by group
w2l_nest(
data = iris, # Input dataset
by = "Species" # Group by Species column
)
#> Species data
#> <fctr> <list>
#> 1: setosa <data.table[50x4]>
#> 2: versicolor <data.table[50x4]>
#> 3: virginica <data.table[50x4]>
# Example 2: Nest specific columns with numeric indices
w2l_nest(
data = iris, # Input dataset
cols2l = 1:4, # Select first 4 columns to nest
by = "Species" # Group by Species column
)
#> name Species data
#> <fctr> <fctr> <list>
#> 1: Sepal.Length setosa <data.table[50x1]>
#> 2: Sepal.Length versicolor <data.table[50x1]>
#> 3: Sepal.Length virginica <data.table[50x1]>
#> 4: Sepal.Width setosa <data.table[50x1]>
#> 5: Sepal.Width versicolor <data.table[50x1]>
#> 6: Sepal.Width virginica <data.table[50x1]>
#> 7: Petal.Length setosa <data.table[50x1]>
#> 8: Petal.Length versicolor <data.table[50x1]>
#> 9: Petal.Length virginica <data.table[50x1]>
#> 10: Petal.Width setosa <data.table[50x1]>
#> 11: Petal.Width versicolor <data.table[50x1]>
#> 12: Petal.Width virginica <data.table[50x1]>
# Example 3: Nest specific columns with column names
w2l_nest(
data = iris, # Input dataset
cols2l = c("Sepal.Length", # Select columns by name
"Sepal.Width",
"Petal.Length"),
by = 5 # Group by column index 5 (Species)
)
#> name Species data
#> <fctr> <fctr> <list>
#> 1: Sepal.Length setosa <data.table[50x2]>
#> 2: Sepal.Length versicolor <data.table[50x2]>
#> 3: Sepal.Length virginica <data.table[50x2]>
#> 4: Sepal.Width setosa <data.table[50x2]>
#> 5: Sepal.Width versicolor <data.table[50x2]>
#> 6: Sepal.Width virginica <data.table[50x2]>
#> 7: Petal.Length setosa <data.table[50x2]>
#> 8: Petal.Length versicolor <data.table[50x2]>
#> 9: Petal.Length virginica <data.table[50x2]>
# Returns similar structure to Example 2