Pivot data from long to wide using a spec
This is a low level interface to pivotting, inspired by the cdata package, that allows you to describe pivotting with a data frame.
pivot_wider_spec( data, spec, names_repair = "check_unique", id_cols = NULL, values_fill = NULL, values_fn = NULL ) build_wider_spec( data, names_from = name, values_from = value, names_prefix = "", names_sep = "_", names_glue = NULL, names_sort = FALSE )
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 |
names_repair |
What happens if the output has invalid column names?
The default, |
id_cols |
< |
values_fill |
Optionally, a (scalar) value that specifies what each
This can be a named list if you want to apply different aggregations to different value columns. |
values_fn |
Optionally, a function applied to the This can be a named list if you want to apply different aggregations to different value columns. |
names_from |
< If |
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 |
# 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)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.