EVOLUTION-MANAGER
Edit File: pivot_wider_spec.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Pivot data from long to wide using a spec</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for pivot_wider_spec {tidyr}"><tr><td>pivot_wider_spec {tidyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Pivot data from long to wide using a spec</h2> <h3>Description</h3> <p>This is a low level interface to pivotting, inspired by the cdata package, that allows you to describe pivotting with a data frame. </p> <h3>Usage</h3> <pre> 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 ) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>data</code></td> <td> <p>A data frame to pivot.</p> </td></tr> <tr valign="top"><td><code>spec</code></td> <td> <p>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. </p> <p>Must be a data frame containing character <code>.name</code> and <code>.value</code> columns. Additional columns in <code>spec</code> should be named to match columns in the long format of the dataset and contain values corresponding to columns pivoted from the wide format. The special <code>.seq</code> variable is used to disambiguate rows internally; it is automatically removed after pivotting.</p> </td></tr> <tr valign="top"><td><code>names_repair</code></td> <td> <p>What happens if the output has invalid column names? The default, <code>"check_unique"</code> is to error if the columns are duplicated. Use <code>"minimal"</code> to allow duplicates in the output, or <code>"unique"</code> to de-duplicated by adding numeric suffixes. See <code><a href="../../vctrs/html/vec_as_names.html">vctrs::vec_as_names()</a></code> for more options.</p> </td></tr> <tr valign="top"><td><code>id_cols</code></td> <td> <p><<code><a href="tidyr_tidy_select.html">tidy-select</a></code>> A set of columns that uniquely identifies each observation. Defaults to all columns in <code>data</code> except for the columns specified in <code>names_from</code> and <code>values_from</code>. Typically used when you have redundant variables, i.e. variables whose values are perfectly correlated with existing variables.</p> </td></tr> <tr valign="top"><td><code>values_fill</code></td> <td> <p>Optionally, a (scalar) value that specifies what each <code>value</code> should be filled in with when missing. </p> <p>This can be a named list if you want to apply different aggregations to different value columns.</p> </td></tr> <tr valign="top"><td><code>values_fn</code></td> <td> <p>Optionally, a function applied to the <code>value</code> in each cell in the output. You will typically use this when the combination of <code>id_cols</code> and <code>value</code> column does not uniquely identify an observation. </p> <p>This can be a named list if you want to apply different aggregations to different value columns.</p> </td></tr> <tr valign="top"><td><code>names_from</code></td> <td> <p><<code><a href="tidyr_tidy_select.html">tidy-select</a></code>> A pair of arguments describing which column (or columns) to get the name of the output column (<code>names_from</code>), and which column (or columns) to get the cell values from (<code>values_from</code>). </p> <p>If <code>values_from</code> contains multiple values, the value will be added to the front of the output column.</p> </td></tr> <tr valign="top"><td><code>values_from</code></td> <td> <p><<code><a href="tidyr_tidy_select.html">tidy-select</a></code>> A pair of arguments describing which column (or columns) to get the name of the output column (<code>names_from</code>), and which column (or columns) to get the cell values from (<code>values_from</code>). </p> <p>If <code>values_from</code> contains multiple values, the value will be added to the front of the output column.</p> </td></tr> <tr valign="top"><td><code>names_prefix</code></td> <td> <p>String added to the start of every variable name. This is particularly useful if <code>names_from</code> is a numeric vector and you want to create syntactic variable names.</p> </td></tr> <tr valign="top"><td><code>names_sep</code></td> <td> <p>If <code>names_from</code> or <code>values_from</code> contains multiple variables, this will be used to join their values together into a single string to use as a column name.</p> </td></tr> <tr valign="top"><td><code>names_glue</code></td> <td> <p>Instead of <code>names_sep</code> and <code>names_prefix</code>, you can supply a glue specification that uses the <code>names_from</code> columns (and special <code>.value</code>) to create custom column names.</p> </td></tr> <tr valign="top"><td><code>names_sort</code></td> <td> <p>Should the column names be sorted? If <code>FALSE</code>, the default, column names are ordered by first appearance.</p> </td></tr> </table> <h3>Examples</h3> <pre> # 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) </pre> <hr /><div style="text-align: center;">[Package <em>tidyr</em> version 1.1.2 <a href="00Index.html">Index</a>]</div> </body></html>