EVOLUTION-MANAGER
Edit File: vector_recycling_rules.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: Recycling rules used by r-lib and the tidyverse</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 vector_recycling_rules {vctrs}"><tr><td>vector_recycling_rules {vctrs}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Recycling rules used by r-lib and the tidyverse</h2> <h3>Description</h3> <p>Recycling describes the concept of repeating elements of one vector to match the size of another. There are two rules that underlie the "tidyverse" recycling rules: </p> <ul> <li><p> Vectors of size 1 will be recycled to the size of any other vector </p> </li> <li><p> Otherwise, all vectors must have the same size </p> </li></ul> <h3>Examples</h3> <p>Vectors of size 1 are recycled to the size of any other vector: </p> <div class="sourceCode r"><pre>tibble(x = 1:3, y = 1L) #> # A tibble: 3 x 2 #> x y #> <int> <int> #> 1 1 1 #> 2 2 1 #> 3 3 1 </pre></div> <p>This includes vectors of size 0: </p> <div class="sourceCode r"><pre>tibble(x = integer(), y = 1L) #> # A tibble: 0 x 2 #> # ... with 2 variables: x <int>, y <int> </pre></div> <p>If vectors aren't size 1, they must all be the same size. Otherwise, an error is thrown: </p> <div class="sourceCode r"><pre>tibble(x = 1:3, y = 4:7) #> Error: #> ! Tibble columns must have compatible sizes. #> * Size 3: Existing data. #> * Size 4: Column `y`. #> i Only values of size one are recycled. </pre></div> <h3>vctrs backend</h3> <p>Packages in r-lib and the tidyverse generally use <code><a href="vec_size.html">vec_size_common()</a></code> and <code><a href="vec_recycle.html">vec_recycle_common()</a></code> as the backends for handling recycling rules. </p> <ul> <li> <p><code>vec_size_common()</code> returns the common size of multiple vectors, after applying the recycling rules </p> </li> <li> <p><code>vec_recycle_common()</code> goes one step further, and actually recycles the vectors to their common size </p> </li></ul> <div class="sourceCode r"><pre>vec_size_common(1:3, "x") #> [1] 3 vec_recycle_common(1:3, "x") #> [[1]] #> [1] 1 2 3 #> #> [[2]] #> [1] "x" "x" "x" vec_size_common(1:3, c("x", "y")) #> Error: #> ! Can't recycle `..1` (size 3) to match `..2` (size 2). </pre></div> <h3>Base R recycling rules</h3> <p>The recycling rules described here are stricter than the ones generally used by base R, which are: </p> <ul> <li><p> If any vector is length 0, the output will be length 0 </p> </li> <li><p> Otherwise, the output will be length <code>max(length_x, length_y)</code>, and a warning will be thrown if the length of the longer vector is not an integer multiple of the length of the shorter vector. </p> </li></ul> <p>We explore the base R rules in detail in <code>vignette("type-size")</code>. </p> <hr /><div style="text-align: center;">[Package <em>vctrs</em> version 0.5.0 <a href="00Index.html">Index</a>]</div> </body></html>