EVOLUTION-MANAGER
Edit File: vec_slice.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: Get or set observations in a vector</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_slice {vctrs}"><tr><td>vec_slice {vctrs}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Get or set observations in a vector</h2> <h3>Description</h3> <p>This provides a common interface to extracting and modifying observations for all vector types, regardless of dimensionality. It is an analog to <code>[</code> that matches <code><a href="vec_size.html">vec_size()</a></code> instead of <code>length()</code>. </p> <h3>Usage</h3> <pre> vec_slice(x, i) vec_slice(x, i) <- value vec_assign(x, i, value, ..., x_arg = "", value_arg = "") </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>A vector</p> </td></tr> <tr valign="top"><td><code>i</code></td> <td> <p>An integer, character or logical vector specifying the locations or names of the observations to get/set. Specify <code>TRUE</code> to index all elements (as in <code>x[]</code>), or <code>NULL</code>, <code>FALSE</code> or <code>integer()</code> to index none (as in <code>x[NULL]</code>).</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>Replacement values. <code>value</code> is cast to the type of <code>x</code>, but only if they have a common type. See below for examples of this rule.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>These dots are for future extensions and must be empty.</p> </td></tr> <tr valign="top"><td><code>x_arg, value_arg</code></td> <td> <p>Argument names for <code>x</code> and <code>value</code>. These are used in error messages to inform the user about the locations of incompatible types and sizes (see <code><a href="vctrs-conditions.html">stop_incompatible_type()</a></code> and <code><a href="vctrs-conditions.html">stop_incompatible_size()</a></code>).</p> </td></tr> </table> <h3>Value</h3> <p>A vector of the same type as <code>x</code>. </p> <h3>Genericity</h3> <p>Support for S3 objects depends on whether the object implements a <code><a href="vec_proxy.html">vec_proxy()</a></code> method. </p> <ul> <li><p> When a <code>vec_proxy()</code> method exists, the proxy is sliced and <code>vec_restore()</code> is called on the result. </p> </li> <li><p> Otherwise <code>vec_slice()</code> falls back to the base generic <code>[</code>. </p> </li></ul> <p>Note that S3 lists are treated as scalars by default, and will cause an error if they don't implement a <code><a href="vec_proxy.html">vec_proxy()</a></code> method. </p> <h3>Differences with base R subsetting</h3> <ul> <li> <p><code>vec_slice()</code> only slices along one dimension. For two-dimensional types, the first dimension is subsetted. </p> </li> <li> <p><code>vec_slice()</code> preserves attributes by default. </p> </li> <li> <p><code style="white-space: pre;">vec_slice<-()</code> is type-stable and always returns the same type as the LHS. </p> </li></ul> <h3>Dependencies</h3> <h4>vctrs dependencies</h4> <ul> <li> <p><code><a href="vec_proxy.html">vec_proxy()</a></code> </p> </li> <li> <p><code><a href="vec_proxy.html">vec_restore()</a></code> </p> </li></ul> <h4>base dependencies</h4> <ul> <li> <p><code>base::`[`</code> </p> </li></ul> <p>If a non-data-frame vector class doesn't have a <code><a href="vec_proxy.html">vec_proxy()</a></code> method, the vector is sliced with <code>[</code> instead. </p> <h3>Examples</h3> <pre> x <- sample(10) x vec_slice(x, 1:3) # You can assign with the infix variant: vec_slice(x, 2) <- 100 x # Or with the regular variant that doesn't modify the original input: y <- vec_assign(x, 3, 500) y x # Slicing objects of higher dimension: vec_slice(mtcars, 1:3) # Type stability -------------------------------------------------- # The assign variant is type stable. It always returns the same # type as the input. x <- 1:5 vec_slice(x, 2) <- 20.0 # `x` is still an integer vector because the RHS was cast to the # type of the LHS: vec_ptype(x) # Compare to `[<-`: x[2] <- 20.0 vec_ptype(x) # Note that the types must be coercible for the cast to happen. # For instance, you can cast a double vector of whole numbers to an # integer vector: vec_cast(1, integer()) # But not fractional doubles: try(vec_cast(1.5, integer())) # For this reason you can't assign fractional values in an integer # vector: x <- 1:3 try(vec_slice(x, 2) <- 1.5) </pre> <hr /><div style="text-align: center;">[Package <em>vctrs</em> version 0.5.0 <a href="00Index.html">Index</a>]</div> </body></html>