Function to expand list columns into additional rows.
Arguments
- data
A data frame to use.
- columns
Character vector containing the names of the columns that will be used for the expansion of the data frame.
- separator
Character vector, containing the separator that is used to distinguish different elements in the list columns.
- unite_with
Character string. Name of
- remove_cols
A logical, indicating if the columns need to be removed from the data set after expansion. Will be ignored if
unite_with
isNULL
, to ensure that the information incolumns
is not lost. Can be a vector of logicals if multiplecolumns
are used for expansion.
Examples
if (FALSE) { # \dontrun{
df <- head(iris, n = 6) |>
dplyr::mutate(
expansion_1 = list("feature.1, feature.2"),
expansion_2 = list("group.1, group.2")
)
# expand the data frame with the values in columns "expansion_1" and "expansion_2":
expand_columns(df, c("expansion_1", "expansion_2"))
# You can also unite the values in the expansion columns with any column in the data frame,
# and remove column "expansion_1" but keep column "expansion_2" afterwards:
expand_columns(df, c("expansion_1", "expansion_2"),
unite_with = "Species", remove_cols = c(TRUE, FALSE))
} # }