EVOLUTION-MANAGER
Edit File: pivot_wider.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</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 {tidyr}"><tr><td>pivot_wider {tidyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Pivot data from long to wide</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#maturing'><img src='figures/lifecycle-maturing.svg' alt='Maturing lifecycle'></a> <p><code>pivot_wider()</code> "widens" data, increasing the number of columns and decreasing the number of rows. The inverse transformation is <code><a href="pivot_longer.html">pivot_longer()</a></code>. </p> <p>Learn more in <code>vignette("pivot")</code>. </p> <h3>Usage</h3> <pre> pivot_wider( data, id_cols = NULL, names_from = name, names_prefix = "", names_sep = "_", names_glue = NULL, names_sort = FALSE, names_repair = "check_unique", values_from = value, values_fill = NULL, values_fn = 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>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>names_from, 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> <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_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>...</code></td> <td> <p>Additional arguments passed on to methods.</p> </td></tr> </table> <h3>Details</h3> <p><code>pivot_wider()</code> is an updated approach to <code><a href="spread.html">spread()</a></code>, designed to be both simpler to use and to handle more use cases. We recommend you use <code>pivot_wider()</code> for new code; <code>spread()</code> isn't going away but is no longer under active development. </p> <h3>See Also</h3> <p><code><a href="pivot_wider_spec.html">pivot_wider_spec()</a></code> to pivot "by hand" with a data frame that defines a pivotting specification. </p> <h3>Examples</h3> <pre> # See vignette("pivot") for examples and explanation fish_encounters fish_encounters %>% pivot_wider(names_from = station, values_from = seen) # Fill in missing values fish_encounters %>% pivot_wider(names_from = station, values_from = seen, values_fill = 0) # Generate column names from multiple variables us_rent_income us_rent_income %>% pivot_wider(names_from = variable, values_from = c(estimate, moe)) # When there are multiple `names_from` or `values_from`, you can use # use `names_sep` or `names_glue` to control the output variable names us_rent_income %>% pivot_wider( names_from = variable, names_sep = ".", values_from = c(estimate, moe) ) us_rent_income %>% pivot_wider( names_from = variable, names_glue = "{variable}_{.value}", values_from = c(estimate, moe) ) # Can perform aggregation with values_fn warpbreaks <- as_tibble(warpbreaks[c("wool", "tension", "breaks")]) warpbreaks warpbreaks %>% pivot_wider( names_from = wool, values_from = breaks, values_fn = mean ) </pre> <hr /><div style="text-align: center;">[Package <em>tidyr</em> version 1.1.2 <a href="00Index.html">Index</a>]</div> </body></html>