A wrapper for tidyr::pivot_wider that creates column names as name_variable instead of variable_name
Source: R/tables.R
pivot_wider_name_first.Rdpivot_wider_name_first() reshapes data from long to wide like
tidyr::pivot_wider(), but it puts the value of names_from first in the
new column names (e.g., Model 1_t instead of t_Model 1). In
apa_flextable(), the names_from values then become column spanners.
Usage
pivot_wider_name_first(
data,
...,
id_cols = NULL,
id_expand = FALSE,
names_from = name,
names_prefix = "",
names_sep = "_",
names_sort = FALSE,
names_vary = "slowest",
names_expand = FALSE,
names_repair = "check_unique",
values_from = value,
values_fill = NULL,
values_fn = NULL,
unused_fn = NULL
)Arguments
- data
A data frame to pivot.
- ...
Additional arguments passed on to methods.
- id_cols
<
tidy-select> A set of columns that uniquely identify each observation. Typically used when you have redundant variables, i.e. variables whose values are perfectly correlated with existing variables.Defaults to all columns in
dataexcept for the columns specified throughnames_fromandvalues_from. If a tidyselect expression is supplied, it will be evaluated ondataafter removing the columns specified throughnames_fromandvalues_from.- id_expand
Should the values in the
id_colscolumns be expanded byexpand()before pivoting? This results in more rows, the output will contain a complete expansion of all possible values inid_cols. Implicit factor levels that aren't represented in the data will become explicit. Additionally, the row values corresponding to the expandedid_colswill be sorted.- names_from, values_from
<
tidy-select> A pair of arguments describing which column (or columns) to get the name of the output column (names_from), and which column (or columns) to get the cell values from (values_from).If
values_fromcontains multiple values, the value will be added to the front of the output column.- names_prefix
String added to the start of every variable name. This is particularly useful if
names_fromis a numeric vector and you want to create syntactic variable names.- names_sep
If
names_fromorvalues_fromcontains multiple variables, this will be used to join their values together into a single string to use as a column name.- names_sort
Should the column names be sorted? If
FALSE, the default, column names are ordered by first appearance.- names_vary
"slowest"(default) to keep the columns of eachnames_fromvalue together, or"fastest"to interleave them (seetidyr::pivot_wider())- names_expand
Should the values in the
names_fromcolumns be expanded byexpand()before pivoting? This results in more columns, the output will contain column names corresponding to a complete expansion of all possible values innames_from. Implicit factor levels that aren't represented in the data will become explicit. Additionally, the column names will be sorted, identical to whatnames_sortwould produce.- names_repair
What happens if the output has invalid column names? The default,
"check_unique"is to error if the columns are duplicated. Use"minimal"to allow duplicates in the output, or"unique"to de-duplicated by adding numeric suffixes. Seevctrs::vec_as_names()for more options.- values_fill
Optionally, a (scalar) value that specifies what each
valueshould be filled in with when missing.This can be a named list if you want to apply different fill values to different value columns.
- values_fn
Optionally, a function applied to the value in each cell in the output. You will typically use this when the combination of
id_colsandnames_fromcolumns does not uniquely identify an observation.This can be a named list if you want to apply different aggregations to different
values_fromcolumns.- unused_fn
Optionally, a function applied to summarize the values from the unused columns (i.e. columns not identified by
id_cols,names_from, orvalues_from).The default drops all unused columns from the result.
This can be a named list if you want to apply different aggregations to different unused columns.
id_colsmust be supplied forunused_fnto be useful, since otherwise all unspecified columns will be consideredid_cols.This is similar to grouping by the
id_colsthen summarizing the unused columns usingunused_fn.
Details
The new column names are made of names_prefix, the value of
names_from, names_sep, and the name of the values_from column, in
that order (e.g., names_prefix = "Model " gives Model 1_t). Only one
names_from column is allowed.
Unlike in tidyr::pivot_wider(), names_vary defaults to "slowest",
which keeps the columns of each names_from value next to each other
(e.g., Model 1_t, Model 1_p, Model 2_t, Model 2_p). Two settings
keep apa_flextable() from making one spanner per names_from value:
names_vary = "fastest", which interleaves the columns (Model 1_t,
Model 2_t, Model 1_p, Model 2_p), and a names_sep other than an
underscore, which apa_flextable() does not split into header rows.
Examples
d <- data.frame(Model = 1:2, t = c(2.1, 0.8), p = c(.04, .45))
pivot_wider_name_first(d, names_from = Model, values_from = c(t, p),
names_prefix = "Model ")
#> # A tibble: 1 × 4
#> `Model 1_t` `Model 1_p` `Model 2_t` `Model 2_p`
#> <dbl> <dbl> <dbl> <dbl>
#> 1 2.1 0.04 0.8 0.45
# For comparison:
tidyr::pivot_wider(d, names_from = Model, values_from = c(t, p))
#> # A tibble: 1 × 4
#> t_1 t_2 p_1 p_2
#> <dbl> <dbl> <dbl> <dbl>
#> 1 2.1 0.8 0.04 0.45