EVOLUTION-MANAGER
Edit File: vec_size.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: Number of observations</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 vec_size {vctrs}"><tr><td>vec_size {vctrs}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Number of observations</h2> <h3>Description</h3> <p><code>vec_size(x)</code> returns the size of a vector. <code>vec_is_empty()</code> returns <code>TRUE</code> if the size is zero, <code>FALSE</code> otherwise. </p> <p>The size is distinct from the <code><a href="../../base/html/length.html">length()</a></code> of a vector because it generalises to the "number of observations" for 2d structures, i.e. it's the number of rows in matrix or a data frame. This definition has the important property that every column of a data frame (even data frame and matrix columns) have the same size. <code>vec_size_common(...)</code> returns the common size of multiple vectors. </p> <p><code>list_sizes()</code> returns an integer vector containing the size of each element of a list. It is nearly equivalent to, but faster than, <code>map_int(x, vec_size)</code>, with the exception that <code>list_sizes()</code> will error on non-list inputs, as defined by <code><a href="vec_is_list.html">vec_is_list()</a></code>. <code>list_sizes()</code> is to <code>vec_size()</code> as <code><a href="../../base/html/lengths.html">lengths()</a></code> is to <code><a href="../../base/html/length.html">length()</a></code>. </p> <h3>Usage</h3> <pre> vec_size(x) vec_size_common( ..., .size = NULL, .absent = 0L, .arg = "", .call = caller_env() ) list_sizes(x) vec_is_empty(x) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, ...</code></td> <td> <p>Vector inputs or <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>.size</code></td> <td> <p>If <code>NULL</code>, the default, the output size is determined by recycling the lengths of all elements of <code>...</code>. Alternatively, you can supply <code>.size</code> to force a known size; in this case, <code>x</code> and <code>...</code> are ignored.</p> </td></tr> <tr valign="top"><td><code>.absent</code></td> <td> <p>The size used when no input is provided, or when all input is <code>NULL</code>. If left as <code>NULL</code> when no input is supplied, an error is thrown.</p> </td></tr> <tr valign="top"><td><code>.arg</code></td> <td> <p>An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.</p> </td></tr> <tr valign="top"><td><code>.call</code></td> <td> <p>The execution environment of a currently running function, e.g. <code>caller_env()</code>. The function will be mentioned in error messages as the source of the error. See the <code>call</code> argument of <code><a href="../../rlang/html/abort.html">abort()</a></code> for more information.</p> </td></tr> </table> <h3>Details</h3> <p>There is no vctrs helper that retrieves the number of columns: as this is a property of the <a href="vec_ptype.html">type</a>. </p> <p><code>vec_size()</code> is equivalent to <code>NROW()</code> but has a name that is easier to pronounce, and throws an error when passed non-vector inputs. </p> <h3>Value</h3> <p>An integer (or double for long vectors). </p> <p><code>vec_size_common()</code> returns <code>.absent</code> if all inputs are <code>NULL</code> or absent, <code>0L</code> by default. </p> <h3>Invariants</h3> <ul> <li> <p><code>vec_size(dataframe)</code> == <code>vec_size(dataframe[[i]])</code> </p> </li> <li> <p><code>vec_size(matrix)</code> == <code>vec_size(matrix[, i, drop = FALSE])</code> </p> </li> <li> <p><code>vec_size(vec_c(x, y))</code> == <code>vec_size(x)</code> + <code>vec_size(y)</code> </p> </li></ul> <h3>The size of NULL</h3> <p>The size of <code>NULL</code> is hard-coded to <code>0L</code> in <code>vec_size()</code>. <code>vec_size_common()</code> returns <code>.absent</code> when all inputs are <code>NULL</code> (if only some inputs are <code>NULL</code>, they are simply ignored). </p> <p>A default size of 0 makes sense because sizes are most often queried in order to compute a total size while assembling a collection of vectors. Since we treat <code>NULL</code> as an absent input by principle, we return the identity of sizes under addition to reflect that an absent input doesn't take up any size. </p> <p>Note that other defaults might make sense under different circumstances. For instance, a default size of 1 makes sense for finding the common size because 1 is the identity of the recycling rules. </p> <h3>Dependencies</h3> <ul> <li> <p><code><a href="vec_proxy.html">vec_proxy()</a></code> </p> </li></ul> <h3>See Also</h3> <p><code><a href="vec_slice.html">vec_slice()</a></code> for a variation of <code>[</code> compatible with <code>vec_size()</code>, and <code><a href="vec_recycle.html">vec_recycle()</a></code> to <a href="vector_recycling_rules.html">recycle</a> vectors to common length. </p> <h3>Examples</h3> <pre> vec_size(1:100) vec_size(mtcars) vec_size(array(dim = c(3, 5, 10))) vec_size_common(1:10, 1:10) vec_size_common(1:10, 1) vec_size_common(integer(), 1) list_sizes(list("a", 1:5, letters)) </pre> <hr /><div style="text-align: center;">[Package <em>vctrs</em> version 0.5.0 <a href="00Index.html">Index</a>]</div> </body></html>