w2l_split {mintyr} | R Documentation |
Reshape Wide Data to Long Format and Split into List
Description
The w2l_split
function reshapes wide-format data into long-format and splits it into a list
by variable names and optional grouping columns. It handles both data.frame
and data.table
objects.
Usage
w2l_split(data, cols2l = NULL, by = NULL, split_type = "dt", sep = "_")
Arguments
data |
|
cols2l |
|
by |
|
split_type |
|
sep |
|
Details
The function melts the specified wide columns into long format and splits the resulting data
into a list based on the variable names and any additional grouping variables specified in by
.
The split data can be in the form of data.table
or data.frame
objects, controlled by the
split_type
parameter.
Both cols2l
and by
parameters accept either column indices or column names, providing flexible ways
to specify the columns for transformation and splitting.
Value
A list of data.table
or data.frame
objects (depending on split_type
), split by variable
names and optional grouping columns.
If
by
isNULL
, returns a list split by variable names only.If
by
is specified, returns a list split by both variable names and grouping variables.
Note
Both
cols2l
andby
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
todata.table
if necessary.The
split_type
parameter controls whether split data aredata.table
("dt"
) ordata.frame
("df"
) objects.If
split_type
is not"dt"
or"df"
, the function will stop with an error.
See Also
Related functions and packages:
-
tidytable::group_split()
Split data frame by groups
Examples
# Example: Wide to long format splitting demonstrations
# Example 1: Basic splitting by Species
w2l_split(
data = iris, # Input dataset
by = "Species" # Split by Species column
) |>
lapply(head) # Show first 6 rows of each split
# Example 2: Split specific columns using numeric indices
w2l_split(
data = iris, # Input dataset
cols2l = 1:3, # Select first 3 columns to split
by = 5 # Split by column index 5 (Species)
) |>
lapply(head) # Show first 6 rows of each split
# Example 3: Split specific columns using column names
list_res <- w2l_split(
data = iris, # Input dataset
cols2l = c("Sepal.Length", # Select columns by name
"Sepal.Width"),
by = "Species" # Split by Species column
)
lapply(list_res, head) # Show first 6 rows of each split
# Returns similar structure to Example 2