Pivot data from wide to long 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_longer_spec( data, spec, names_repair = "check_unique", values_drop_na = FALSE, values_ptypes = list(), values_transform = list() ) build_longer_spec( data, cols, names_to = "name", values_to = "value", names_prefix = NULL, names_sep = NULL, names_pattern = NULL, names_ptypes = NULL, names_transform = NULL )
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 column names turns into columns in the result. Must be a data frame containing character |
names_repair |
What happens if the output has invalid column names?
The default, |
values_drop_na |
If |
values_ptypes |
A list of column name-prototype pairs.
A prototype (or ptype for short) is a zero-length vector (like |
values_transform |
A list of column name-function pairs.
Use these arguments if you need to change the types of specific columns.
For example, If not specified, the type of the columns generated from |
cols |
< |
names_to |
A string specifying the name of the column to create
from the data stored in the column names of Can be a character vector, creating multiple columns, if
|
values_to |
A string specifying the name of the column to create
from the data stored in cell values. If |
names_prefix |
A regular expression used to remove matching text from the start of each variable name. |
names_sep |
If
If these arguments do not give you enough control, use
|
names_pattern |
If
If these arguments do not give you enough control, use
|
names_ptypes |
A list of column name-prototype pairs.
A prototype (or ptype for short) is a zero-length vector (like |
names_transform |
A list of column name-function pairs.
Use these arguments if you need to change the types of specific columns.
For example, If not specified, the type of the columns generated from |
# See vignette("pivot") for examples and explanation # Use `build_longer_spec()` to build `spec` using similar syntax to `pivot_longer()` # and run `pivot_longer_spec()` based on `spec`. spec <- relig_income %>% build_longer_spec( cols = !religion, names_to = "income", values_to = "count" ) spec pivot_longer_spec(relig_income, spec) # Is equivalent to: relig_income %>% pivot_longer( cols = !religion, names_to = "income", values_to = "count")
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.