EVOLUTION-MANAGER
Edit File: chop.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: Chop and unchop</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 chop {tidyr}"><tr><td>chop {tidyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Chop and unchop</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#maturing'><img src='figures/lifecycle-maturing.svg' alt='Maturing lifecycle'></a> <p>Chopping and unchopping preserve the width of a data frame, changing its length. <code>chop()</code> makes <code>df</code> shorter by converting rows within each group into list-columns. <code>unchop()</code> makes <code>df</code> longer by expanding list-columns so that each element of the list-column gets its own row in the output. <code>chop()</code> and <code>unchop()</code> are building blocks for more complicated functions (like <code><a href="nest.html">unnest()</a></code>, <code><a href="hoist.html">unnest_longer()</a></code>, and <code><a href="hoist.html">unnest_wider()</a></code>) and are generally more suitable for programming than interactive data analysis. </p> <h3>Usage</h3> <pre> chop(data, cols) unchop(data, cols, keep_empty = FALSE, ptype = 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>cols</code></td> <td> <p><<code><a href="tidyr_tidy_select.html">tidy-select</a></code>> Columns to chop or unchop (automatically quoted). </p> <p>For <code>unchop()</code>, each column should be a list-column containing generalised vectors (e.g. any mix of <code>NULL</code>s, atomic vector, S3 vectors, a lists, or data frames).</p> </td></tr> <tr valign="top"><td><code>keep_empty</code></td> <td> <p>By default, you get one row of output for each element of the list your unchopping/unnesting. This means that if there's a size-0 element (like <code>NULL</code> or an empty data frame), that entire row will be dropped from the output. If you want to preserve all rows, use <code>keep_empty = TRUE</code> to replace size-0 elements with a single row of missing values.</p> </td></tr> <tr valign="top"><td><code>ptype</code></td> <td> <p>Optionally, supply a data frame prototype for the output <code>cols</code>, overriding the default that will be guessed from the combination of individual values.</p> </td></tr> </table> <h3>Details</h3> <p>Generally, unchopping is more useful than chopping because it simplifies a complex data structure, and <code><a href="nest.html">nest()</a></code>ing is usually more appropriate that <code>chop()</code>ing' since it better preserves the connections between observations. </p> <p><code>chop()</code> creates list-columns of class <code><a href="../../vctrs/html/list_of.html">vctrs::list_of()</a></code> to ensure consistent behaviour when the chopped data frame is emptied. For instance this helps getting back the original column types after the roundtrip chop and unchop. Because <code style="white-space: pre;"><list_of></code> keeps tracks of the type of its elements, <code>unchop()</code> is able to reconstitute the correct vector type even for empty list-columns. </p> <h3>Examples</h3> <pre> # Chop ============================================================== df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1) # Note that we get one row of output for each unique combination of # non-chopped variables df %>% chop(c(y, z)) # cf nest df %>% nest(data = c(y, z)) # Unchop ============================================================ df <- tibble(x = 1:4, y = list(integer(), 1L, 1:2, 1:3)) df %>% unchop(y) df %>% unchop(y, keep_empty = TRUE) # Incompatible types ------------------------------------------------- # If the list-col contains types that can not be natively df <- tibble(x = 1:2, y = list("1", 1:3)) try(df %>% unchop(y)) # Unchopping data frames ----------------------------------------------------- # Unchopping a list-col of data frames must generate a df-col because # unchop leaves the column names unchanged df <- tibble(x = 1:3, y = list(NULL, tibble(x = 1), tibble(y = 1:2))) df %>% unchop(y) df %>% unchop(y, keep_empty = TRUE) </pre> <hr /><div style="text-align: center;">[Package <em>tidyr</em> version 1.1.2 <a href="00Index.html">Index</a>]</div> </body></html>