EVOLUTION-MANAGER
Edit File: spread.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: Spread a key-value pair across multiple columns</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 spread {tidyr}"><tr><td>spread {tidyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Spread a key-value pair across multiple columns</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#retired'><img src='figures/lifecycle-retired.svg' alt='Retired lifecycle'></a> <p>Development on <code>spread()</code> is complete, and for new code we recommend switching to <code>pivot_wider()</code>, which is easier to use, more featureful, and still under active development. <code>df %>% spread(key, value)</code> is equivalent to <code>df %>% pivot_wider(names_from = key, values_from = value)</code> </p> <p>See more details in <code>vignette("pivot")</code>. </p> <h3>Usage</h3> <pre> spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>data</code></td> <td> <p>A data frame.</p> </td></tr> <tr valign="top"><td><code>key, value</code></td> <td> <p>Column names or positions. This is passed to <code><a href="../../tidyselect/html/vars_pull.html">tidyselect::vars_pull()</a></code>. </p> <p>These arguments are passed by expression and support <a href="../../rlang/html/nse-force.html">quasiquotation</a> (you can unquote column names or column positions).</p> </td></tr> <tr valign="top"><td><code>fill</code></td> <td> <p>If set, missing values will be replaced with this value. Note that there are two types of missingness in the input: explicit missing values (i.e. <code>NA</code>), and implicit missings, rows that simply aren't present. Both types of missing value will be replaced by <code>fill</code>.</p> </td></tr> <tr valign="top"><td><code>convert</code></td> <td> <p>If <code>TRUE</code>, <code><a href="../../utils/html/type.convert.html">type.convert()</a></code> with <code>asis = TRUE</code> will be run on each of the new columns. This is useful if the value column was a mix of variables that was coerced to a string. If the class of the value column was factor or date, note that will not be true of the new columns that are produced, which are coerced to character before type conversion.</p> </td></tr> <tr valign="top"><td><code>drop</code></td> <td> <p>If <code>FALSE</code>, will keep factor levels that don't appear in the data, filling in missing combinations with <code>fill</code>.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>If <code>NULL</code>, the column names will be taken from the values of <code>key</code> variable. If non-<code>NULL</code>, the column names will be given by <code>"<key_name><sep><key_value>"</code>.</p> </td></tr> </table> <h3>Examples</h3> <pre> library(dplyr) stocks <- data.frame( time = as.Date('2009-01-01') + 0:9, X = rnorm(10, 0, 1), Y = rnorm(10, 0, 2), Z = rnorm(10, 0, 4) ) stocksm <- stocks %>% gather(stock, price, -time) stocksm %>% spread(stock, price) stocksm %>% spread(time, price) # Spread and gather are complements df <- data.frame(x = c("a", "b"), y = c(3, 4), z = c(5, 6)) df %>% spread(x, y) %>% gather("x", "y", a:b, na.rm = TRUE) # Use 'convert = TRUE' to produce variables of mixed type df <- data.frame(row = rep(c(1, 51), each = 3), var = c("Sepal.Length", "Species", "Species_num"), value = c(5.1, "setosa", 1, 7.0, "versicolor", 2)) df %>% spread(var, value) %>% str df %>% spread(var, value, convert = TRUE) %>% str </pre> <hr /><div style="text-align: center;">[Package <em>tidyr</em> version 1.1.2 <a href="00Index.html">Index</a>]</div> </body></html>