w2l_split | R Documentation |
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.
w2l_split(data, cols2l = NULL, by = NULL, split_type = "dt", sep = "_")
data |
|
cols2l |
|
by |
|
split_type |
|
sep |
|
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.
A list of data.table
or data.frame
objects (depending on split_type
), split by variable
names and optional grouping columns.
If by
is NULL
, returns a list split by variable names only.
If by
is specified, returns a list split by both variable names and grouping variables.
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 split_type
parameter controls whether split data are data.table
("dt"
) or data.frame
("df"
) objects.
If split_type
is not "dt"
or "df"
, the function will stop with an error.
Related functions and packages:
tidytable::group_split()
Split data frame by groups
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.