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#> # A tibble: 10 × 3 #> .name .value income #> <chr> <chr> <chr> #> 1 <$10k count <$10k #> 2 $10-20k count $10-20k #> 3 $20-30k count $20-30k #> 4 $30-40k count $30-40k #> 5 $40-50k count $40-50k #> 6 $50-75k count $50-75k #> 7 $75-100k count $75-100k #> 8 $100-150k count $100-150k #> 9 >150k count >150k #> 10 Don't know/refused count Don't know/refusedpivot_longer_spec(relig_income, spec)#> # A tibble: 180 × 3 #> religion income count #> <chr> <chr> <dbl> #> 1 Agnostic <$10k 27 #> 2 Agnostic $10-20k 34 #> 3 Agnostic $20-30k 60 #> 4 Agnostic $30-40k 81 #> 5 Agnostic $40-50k 76 #> 6 Agnostic $50-75k 137 #> 7 Agnostic $75-100k 122 #> 8 Agnostic $100-150k 109 #> 9 Agnostic >150k 84 #> 10 Agnostic Don't know/refused 96 #> # … with 170 more rows# Is equivalent to: relig_income %>% pivot_longer( cols = !religion, names_to = "income", values_to = "count")#> # A tibble: 180 × 3 #> religion income count #> <chr> <chr> <dbl> #> 1 Agnostic <$10k 27 #> 2 Agnostic $10-20k 34 #> 3 Agnostic $20-30k 60 #> 4 Agnostic $30-40k 81 #> 5 Agnostic $40-50k 76 #> 6 Agnostic $50-75k 137 #> 7 Agnostic $75-100k 122 #> 8 Agnostic $100-150k 109 #> 9 Agnostic >150k 84 #> 10 Agnostic Don't know/refused 96 #> # … with 170 more rows