pivot_wider_spec | R Documentation |
This is a low level interface to pivoting, inspired by the cdata package, that allows you to describe pivoting with a data frame.
pivot_wider_spec(
data,
spec,
...,
names_repair = "check_unique",
id_cols = NULL,
id_expand = FALSE,
values_fill = NULL,
values_fn = NULL,
unused_fn = NULL,
error_call = current_env()
)
build_wider_spec(
data,
...,
names_from = name,
values_from = value,
names_prefix = "",
names_sep = "_",
names_glue = NULL,
names_sort = FALSE,
names_vary = "fastest",
names_expand = FALSE,
error_call = current_env()
)
data |
A data frame to pivot. |
spec |
A specification data frame. This is useful for more complex pivots because it gives you greater control on how metadata stored in the columns become column names in the result. Must be a data frame containing character |
... |
These dots are for future extensions and must be empty. |
names_repair |
What happens if the output has invalid column names?
The default, |
id_cols |
< |
id_expand |
Should the values in the |
values_fill |
Optionally, a (scalar) value that specifies what each
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
This can be a named list if you want to apply different aggregations
to different |
unused_fn |
Optionally, a function applied to summarize the values from
the unused columns (i.e. columns not identified by 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.
This is similar to grouping by the |
error_call |
The execution environment of a currently
running function, e.g. |
names_from , values_from |
< If |
names_prefix |
String added to the start of every variable name. This is
particularly useful if |
names_sep |
If |
names_glue |
Instead of |
names_sort |
Should the column names be sorted? If |
names_vary |
When
|
names_expand |
Should the values in the |
# See vignette("pivot") for examples and explanation
us_rent_income
spec1 <- us_rent_income %>%
build_wider_spec(names_from = variable, values_from = c(estimate, moe))
spec1
us_rent_income %>%
pivot_wider_spec(spec1)
# Is equivalent to
us_rent_income %>%
pivot_wider(names_from = variable, values_from = c(estimate, moe))
# `pivot_wider_spec()` provides more control over column names and output format
# instead of creating columns with estimate_ and moe_ prefixes,
# keep original variable name for estimates and attach _moe as suffix
spec2 <- tibble(
.name = c("income", "rent", "income_moe", "rent_moe"),
.value = c("estimate", "estimate", "moe", "moe"),
variable = c("income", "rent", "income", "rent")
)
us_rent_income %>%
pivot_wider_spec(spec2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.