EVOLUTION-MANAGER
Edit File: vector.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: Vectors</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 {base}"><tr><td>vector {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Vectors</h2> <h3>Description</h3> <p><code>vector</code> produces a vector of the given length and mode. </p> <p><code>as.vector</code>, a generic, attempts to coerce its argument into a vector of mode <code>mode</code> (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed. </p> <p><code>is.vector</code> returns <code>TRUE</code> if <code>x</code> is a vector of the specified mode having no attributes <em>other than names</em>. It returns <code>FALSE</code> otherwise. </p> <h3>Usage</h3> <pre> vector(mode = "logical", length = 0) as.vector(x, mode = "any") is.vector(x, mode = "any") </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>mode</code></td> <td> <p>character string naming an atomic mode or <code>"list"</code> or <code>"expression"</code> or (except for <code>vector</code>) <code>"any"</code>. Currently, <code>is.vector()</code> allows any type (see <code><a href="typeof.html">typeof</a></code>) for <code>mode</code>, and when mode is not <code>"any"</code>, <code>is.vector(x, mode)</code> is almost the same as <code>typeof(x) == mode</code>.</p> </td></tr></table> <table summary="R argblock"> <tr valign="top"><td><code>length</code></td> <td> <p>a non-negative integer specifying the desired length. For a long vector, i.e., <code>length > .Machine$integer.max</code>, it has to be of type <code>"double"</code>. Supplying an argument of length other than one is an error.</p> </td></tr> <tr valign="top"><td><code>x</code></td> <td> <p>an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object.</p> </td></tr> </table> <h3>Details</h3> <p>The atomic modes are <code>"logical"</code>, <code>"integer"</code>, <code>"numeric"</code> (synonym <code>"double"</code>), <code>"complex"</code>, <code>"character"</code> and <code>"raw"</code>. </p> <p>If <code>mode = "any"</code>, <code>is.vector</code> may return <code>TRUE</code> for the atomic modes, <code><a href="list.html">list</a></code> and <code><a href="expression.html">expression</a></code>. For any <code>mode</code>, it will return <code>FALSE</code> if <code>x</code> has any attributes except names. (This is incompatible with S.) On the other hand, <code>as.vector</code> removes <em>all</em> attributes including names for results of atomic mode (but not those of mode <code>"list"</code> nor <code>"expression"</code>). </p> <p>Note that factors are <em>not</em> vectors; <code>is.vector</code> returns <code>FALSE</code> and <code>as.vector</code> converts a factor to a character vector for <code>mode = "any"</code>. </p> <h3>Value</h3> <p>For <code>vector</code>, a vector of the given length and mode. Logical vector elements are initialized to <code>FALSE</code>, numeric vector elements to <code>0</code>, character vector elements to <code>""</code>, raw vector elements to <code>nul</code> bytes and list/expression elements to <code>NULL</code>. </p> <p>For <code>as.vector</code>, a vector (atomic or of type list or expression). All attributes are removed from the result if it is of an atomic mode, but not in general for a list result. The default method handles 24 input types and 12 values of <code>type</code>: the details of most coercions are undocumented and subject to change. </p> <p>For <code>is.vector</code>, <code>TRUE</code> or <code>FALSE</code>. <code>is.vector(x, mode = "numeric")</code> can be true for vectors of types <code>"integer"</code> or <code>"double"</code> whereas <code>is.vector(x, mode = "double")</code> can only be true for those of type <code>"double"</code>. </p> <h3>Methods for <code>as.vector()</code></h3> <p>Writers of methods for <code>as.vector</code> need to take care to follow the conventions of the default method. In particular </p> <ul> <li><p> Argument <code>mode</code> can be <code>"any"</code>, any of the atomic modes, <code>"list"</code>, <code>"expression"</code>, <code>"symbol"</code>, <code>"pairlist"</code> or one of the aliases <code>"double"</code> and <code>"name"</code>. </p> </li> <li><p> The return value should be of the appropriate mode. For <code>mode = "any"</code> this means an atomic vector or list. </p> </li> <li><p> Attributes should be treated appropriately: in particular when the result is an atomic vector there should be no attributes, not even names. </p> </li> <li> <p><code>is.vector(as.vector(x, m), m)</code> should be true for any mode <code>m</code>, including the default <code>"any"</code>. </p> </li></ul> <h3>Note</h3> <p><code>as.vector</code> and <code>is.vector</code> are quite distinct from the meaning of the formal class <code>"vector"</code> in the <span class="pkg">methods</span> package, and hence <code><a href="../../methods/html/as.html">as</a>(x, "vector")</code> and <code><a href="../../methods/html/is.html">is</a>(x, "vector")</code>. </p> <p>Note that <code>as.vector(x)</code> is not necessarily a null operation if <code>is.vector(x)</code> is true: any names will be removed from an atomic vector. </p> <p>Non-vector <code>mode</code>s <code>"symbol"</code> (synonym <code>"name"</code>) and <code>"pairlist"</code> are accepted but have long been undocumented: they are used to implement <code><a href="name.html">as.name</a></code> and <code><a href="list.html">as.pairlist</a></code>, and those functions should preferably be used directly. None of the description here applies to those <code>mode</code>s: see the help for the preferred forms. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="c.html">c</a></code>, <code><a href="numeric.html">is.numeric</a></code>, <code><a href="list.html">is.list</a></code>, etc. </p> <h3>Examples</h3> <pre> df <- data.frame(x = 1:3, y = 5:7) ## Error: try(as.vector(data.frame(x = 1:3, y = 5:7), mode = "numeric")) x <- c(a = 1, b = 2) is.vector(x) as.vector(x) all.equal(x, as.vector(x)) ## FALSE ###-- All the following are TRUE: is.list(df) ! is.vector(df) ! is.vector(df, mode = "list") is.vector(list(), mode = "list") </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>