EVOLUTION-MANAGER
Edit File: pivot_longer_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 wide to long 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_longer_spec {tidyr}"><tr><td>pivot_longer_spec {tidyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Pivot data from wide to long 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_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 ) </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 column names turns into columns 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>values_drop_na</code></td> <td> <p>If <code>TRUE</code>, will drop rows that contain only <code>NA</code>s in the <code>value_to</code> column. This effectively converts explicit missing values to implicit missing values, and should generally be used only when missing values in <code>data</code> were created by its structure.</p> </td></tr> <tr valign="top"><td><code>values_ptypes</code></td> <td> <p>A list of column name-prototype pairs. A prototype (or ptype for short) is a zero-length vector (like <code>integer()</code> or <code>numeric()</code>) that defines the type, class, and attributes of a vector. Use these arguments to confirm that the created columns are the types that you expect. </p> <p>If not specified, the type of the columns generated from <code>names_to</code> will be character, and the type of the variables generated from <code>values_to</code> will be the common type of the input columns used to generate them.</p> </td></tr> <tr valign="top"><td><code>values_transform</code></td> <td> <p>A list of column name-function pairs. Use these arguments if you need to change the type of specific columns. For example, <code>names_transform = list(week = as.integer)</code> would convert a character week variable to an integer.</p> </td></tr> <tr valign="top"><td><code>cols</code></td> <td> <p><<code><a href="tidyr_tidy_select.html">tidy-select</a></code>> Columns to pivot into longer format.</p> </td></tr> <tr valign="top"><td><code>names_to</code></td> <td> <p>A string specifying the name of the column to create from the data stored in the column names of <code>data</code>. </p> <p>Can be a character vector, creating multiple columns, if <code>names_sep</code> or <code>names_pattern</code> is provided. In this case, there are two special values you can take advantage of: </p> <ul> <li> <p><code>NA</code> will discard that component of the name. </p> </li> <li> <p><code>.value</code> indicates that component of the name defines the name of the column containing the cell values, overriding <code>values_to</code>. </p> </li></ul> </td></tr> <tr valign="top"><td><code>values_to</code></td> <td> <p>A string specifying the name of the column to create from the data stored in cell values. If <code>names_to</code> is a character containing the special <code>.value</code> sentinel, this value will be ignored, and the name of the value column will be derived from part of the existing column names.</p> </td></tr> <tr valign="top"><td><code>names_prefix</code></td> <td> <p>A regular expression used to remove matching text from the start of each variable name.</p> </td></tr> <tr valign="top"><td><code>names_sep</code></td> <td> <p>If <code>names_to</code> contains multiple values, these arguments control how the column name is broken up. </p> <p><code>names_sep</code> takes the same specification as <code><a href="separate.html">separate()</a></code>, and can either be a numeric vector (specifying positions to break on), or a single string (specifying a regular expression to split on). </p> <p><code>names_pattern</code> takes the same specification as <code><a href="extract.html">extract()</a></code>, a regular expression containing matching groups (<code style="white-space: pre;">()</code>). </p> <p>If these arguments do not give you enough control, use <code>pivot_longer_spec()</code> to create a spec object and process manually as needed.</p> </td></tr> <tr valign="top"><td><code>names_pattern</code></td> <td> <p>If <code>names_to</code> contains multiple values, these arguments control how the column name is broken up. </p> <p><code>names_sep</code> takes the same specification as <code><a href="separate.html">separate()</a></code>, and can either be a numeric vector (specifying positions to break on), or a single string (specifying a regular expression to split on). </p> <p><code>names_pattern</code> takes the same specification as <code><a href="extract.html">extract()</a></code>, a regular expression containing matching groups (<code style="white-space: pre;">()</code>). </p> <p>If these arguments do not give you enough control, use <code>pivot_longer_spec()</code> to create a spec object and process manually as needed.</p> </td></tr> <tr valign="top"><td><code>names_ptypes</code></td> <td> <p>A list of column name-prototype pairs. A prototype (or ptype for short) is a zero-length vector (like <code>integer()</code> or <code>numeric()</code>) that defines the type, class, and attributes of a vector. Use these arguments to confirm that the created columns are the types that you expect. </p> <p>If not specified, the type of the columns generated from <code>names_to</code> will be character, and the type of the variables generated from <code>values_to</code> will be the common type of the input columns used to generate them.</p> </td></tr> <tr valign="top"><td><code>names_transform</code></td> <td> <p>A list of column name-function pairs. Use these arguments if you need to change the type of specific columns. For example, <code>names_transform = list(week = as.integer)</code> would convert a character week variable to an integer.</p> </td></tr> </table> <h3>Examples</h3> <pre> # 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") </pre> <hr /><div style="text-align: center;">[Package <em>tidyr</em> version 1.1.2 <a href="00Index.html">Index</a>]</div> </body></html>