EVOLUTION-MANAGER
Edit File: numeric.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: Numeric 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 numeric {base}"><tr><td>numeric {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Numeric Vectors</h2> <h3>Description</h3> <p>Creates or coerces objects of type <code>"numeric"</code>. <code>is.numeric</code> is a more general test of an object being interpretable as numbers. </p> <h3>Usage</h3> <pre> numeric(length = 0) as.numeric(x, ...) is.numeric(x) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>length</code></td> <td> <p>A non-negative integer specifying the desired length. Double values will be coerced to integer: supplying an argument of length other than one is an error.</p> </td></tr> <tr valign="top"><td><code>x</code></td> <td> <p>object to be coerced or tested.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments passed to or from other methods.</p> </td></tr> </table> <h3>Details</h3> <p><code>numeric</code> is identical to <code><a href="double.html">double</a></code> (and <code>real</code>). It creates a double-precision vector of the specified length with each element equal to <code>0</code>. </p> <p><code>as.numeric</code> is a generic function, but S3 methods must be written for <code><a href="double.html">as.double</a></code>. It is identical to <code>as.double</code>. </p> <p><code>is.numeric</code> is an <a href="InternalMethods.html">internal generic</a> <code>primitive</code> function: you can write methods to handle specific classes of objects, see <a href="InternalMethods.html">InternalMethods</a>. It is <strong>not</strong> the same as <code><a href="double.html">is.double</a></code>. Factors are handled by the default method, and there are methods for classes <code>"<a href="Dates.html">Date</a>"</code>, <code>"<a href="DateTimeClasses.html">POSIXt</a>"</code> and <code>"<a href="difftime.html">difftime</a>"</code> (all of which return false). Methods for <code>is.numeric</code> should only return true if the base type of the class is <code>double</code> or <code>integer</code> <em>and</em> values can reasonably be regarded as numeric (e.g., arithmetic on them makes sense, and comparison should be done via the base type). </p> <h3>Value</h3> <p>for <code>numeric</code> and <code>as.numeric</code> see <code><a href="double.html">double</a></code>. </p> <p>The default method for <code>is.numeric</code> returns <code>TRUE</code> if its argument is of <a href="mode.html">mode</a> <code>"numeric"</code> (<a href="typeof.html">type</a> <code>"double"</code> or type <code>"integer"</code>) and not a factor, and <code>FALSE</code> otherwise. That is, <code>is.integer(x) || is.double(x)</code>, or <code>(mode(x) == "numeric") && !is.factor(x)</code>. </p> <h3>Warning</h3> <p>If <code>x</code> is a <code><a href="factor.html">factor</a></code>, <code>as.numeric</code> will return the underlying numeric (integer) representation, which is often meaningless as it may not correspond to the <code>factor</code> <code><a href="levels.html">levels</a></code>, see the ‘Warning’ section in <code><a href="factor.html">factor</a></code> (and the 2nd example below). </p> <h3>S4 methods</h3> <p><code>as.numeric</code> and <code>is.numeric</code> are internally S4 generic and so methods can be set for them <em>via</em> <code>setMethod</code>. </p> <p>To ensure that <code>as.numeric</code> and <code>as.double</code> remain identical, S4 methods can only be set for <code>as.numeric</code>. </p> <h3>Note on names</h3> <p>It is a historical anomaly that <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> has two names for its floating-point vectors, <code><a href="double.html">double</a></code> and <code><a href="numeric.html">numeric</a></code> (and formerly had <code>real</code>). </p> <p><code>double</code> is the name of the <a href="typeof.html">type</a>. <code>numeric</code> is the name of the <a href="mode.html">mode</a> and also of the implicit <a href="class.html">class</a>. As an S4 formal class, use <code>"numeric"</code>. </p> <p>The potential confusion is that <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> has used <em><a href="mode.html">mode</a></em> <code>"numeric"</code> to mean ‘double or integer’, which conflicts with the S4 usage. Thus <code>is.numeric</code> tests the mode, not the class, but <code>as.numeric</code> (which is identical to <code>as.double</code>) coerces to the class. </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="double.html">double</a></code>, <code><a href="integer.html">integer</a></code>, <code><a href="mode.html">storage.mode</a></code>. </p> <h3>Examples</h3> <pre> as.numeric(c("-.1"," 2.7 ","B")) # (-0.1, 2.7, NA) + warning as.numeric(factor(5:10)) # not what you'd expect f <- factor(1:5) ## what you typically meant and want: as.numeric(as.character(f)) ## the same, considerably (for long factors) more efficient: as.numeric(levels(f))[f] </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>