Split a data frame into a non-nested list of subsets.
h_split_by_subgroups(data, subgroups, groups_lists = list())
A list with subset data (df
) and metadata about the subset (df_labels
).
(data.frame
)
dataset to split.
(character
)
names of factor variables from data
used to create subsets.
Unused levels not present in data
are dropped. Note that the order in this vector
determines the order in the downstream table.
(named list
of list
)
optionally contains for each subgroups
variable a
list, which specifies the new group levels via the names and the
levels that belong to it in the character vectors that are elements of the list.
Main functionality is to prepare data for use in forest plot layouts.
df <- data.frame(
x = c(1:5),
y = factor(c("A", "B", "A", "B", "A"), levels = c("A", "B", "C")),
z = factor(c("C", "C", "D", "D", "D"), levels = c("D", "C"))
)
formatters::var_labels(df) <- paste("label for", names(df))
h_split_by_subgroups(
data = df,
subgroups = c("y", "z")
)
h_split_by_subgroups(
data = df,
subgroups = c("y", "z"),
groups_lists = list(
y = list("AB" = c("A", "B"), "C" = "C")
)
)
Run the code above in your browser using DataLab