| 42333 |
ripley |
1 |
% File src/library/base/man/numeric.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 81930 |
maechler |
3 |
% Copyright 1995-2022 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{numeric}
|
|
|
7 |
\title{Numeric Vectors}
|
| 56186 |
murdoch |
8 |
\alias{numeric}
|
| 880 |
hornik |
9 |
\alias{as.numeric}
|
|
|
10 |
\alias{is.numeric}
|
| 41620 |
ripley |
11 |
\alias{is.numeric.Date}
|
|
|
12 |
\alias{is.numeric.POSIXt}
|
| 30825 |
ripley |
13 |
\description{
|
| 41620 |
ripley |
14 |
Creates or coerces objects of type \code{"numeric"}.
|
|
|
15 |
\code{is.numeric} is a more general test of an object being
|
|
|
16 |
interpretable as numbers.
|
| 30825 |
ripley |
17 |
}
|
| 2 |
r |
18 |
\usage{
|
| 880 |
hornik |
19 |
numeric(length = 0)
|
| 10525 |
ripley |
20 |
as.numeric(x, \dots)
|
| 2 |
r |
21 |
is.numeric(x)
|
|
|
22 |
}
|
| 15518 |
ripley |
23 |
\arguments{
|
| 85065 |
smeyer |
24 |
\item{length}{a non-negative integer specifying the desired length.
|
| 56158 |
ripley |
25 |
Double values will be coerced to integer:
|
| 56156 |
ripley |
26 |
supplying an argument of length other than one is an error.}
|
| 15518 |
ripley |
27 |
\item{x}{object to be coerced or tested.}
|
|
|
28 |
\item{\dots}{further arguments passed to or from other methods.}
|
|
|
29 |
}
|
| 30825 |
ripley |
30 |
\details{
|
| 81930 |
maechler |
31 |
\code{numeric} is identical to \code{\link{double}}.
|
| 41620 |
ripley |
32 |
It creates a double-precision vector of the specified length with each
|
|
|
33 |
element equal to \code{0}.
|
| 30825 |
ripley |
34 |
|
| 41620 |
ripley |
35 |
\code{as.numeric} is a generic function, but S3 methods must be
|
| 61812 |
ripley |
36 |
written for \code{\link{as.double}}. It is identical to \code{as.double}.
|
| 41620 |
ripley |
37 |
|
| 51318 |
ripley |
38 |
\code{is.numeric} is an \link{internal generic} \code{primitive}
|
|
|
39 |
function: you can write methods to handle specific classes of objects,
|
|
|
40 |
see \link{InternalMethods}. It is \strong{not} the same as
|
|
|
41 |
\code{\link{is.double}}. Factors are handled by the default method,
|
| 87715 |
maechler |
42 |
and there are methods for classes \code{"\link{complex}"}, \code{"\link{Date}"},
|
| 51318 |
ripley |
43 |
\code{"\link{POSIXt}"} and \code{"\link{difftime}"} (all of which
|
|
|
44 |
return false). Methods for \code{is.numeric} should only return true
|
|
|
45 |
if the base type of the class is \code{double} or \code{integer}
|
|
|
46 |
\emph{and} values can reasonably be regarded as numeric
|
| 66444 |
hornik |
47 |
(e.g., arithmetic on them makes sense, and comparison should be done
|
| 51318 |
ripley |
48 |
via the base type).
|
| 30825 |
ripley |
49 |
}
|
|
|
50 |
\value{
|
| 41620 |
ripley |
51 |
for \code{numeric} and \code{as.numeric} see \code{\link{double}}.
|
| 61433 |
ripley |
52 |
|
| 40667 |
ripley |
53 |
The default method for \code{is.numeric} returns \code{TRUE}
|
|
|
54 |
if its argument is of \link{mode} \code{"numeric"}
|
|
|
55 |
(\link{type} \code{"double"} or type \code{"integer"}) and not a
|
|
|
56 |
factor, and \code{FALSE} otherwise. That is,
|
|
|
57 |
\code{is.integer(x) || is.double(x)}, or
|
|
|
58 |
\code{(mode(x) == "numeric") && !is.factor(x)}.
|
| 2 |
r |
59 |
}
|
| 69847 |
maechler |
60 |
\section{Warning}{
|
|
|
61 |
If \code{x} is a \code{\link{factor}}, \code{as.numeric} will return
|
|
|
62 |
the underlying numeric (integer) representation, which is often
|
|
|
63 |
meaningless as it may not correspond to the \code{factor}
|
|
|
64 |
\code{\link{levels}}, see the \sQuote{Warning} section in
|
|
|
65 |
\code{\link{factor}} (and the 2nd example below).
|
|
|
66 |
}
|
| 41620 |
ripley |
67 |
\section{S4 methods}{
|
|
|
68 |
\code{as.numeric} and \code{is.numeric} are internally S4 generic and
|
|
|
69 |
so methods can be set for them \emph{via} \code{setMethod}.
|
| 25376 |
ripley |
70 |
|
| 61812 |
ripley |
71 |
To ensure that \code{as.numeric} and \code{as.double}
|
| 41620 |
ripley |
72 |
remain identical, S4 methods can only be set for \code{as.numeric}.
|
| 2 |
r |
73 |
}
|
| 43445 |
ripley |
74 |
%% keep next the same in double.Rd & numeric.Rd
|
|
|
75 |
\section{Note on names}{
|
| 61810 |
ripley |
76 |
It is a historical anomaly that \R has two names for its
|
| 87653 |
smeyer |
77 |
floating-point vectors, \code{double} and \code{numeric}
|
| 61810 |
ripley |
78 |
(and formerly had \code{real}).
|
| 43445 |
ripley |
79 |
|
|
|
80 |
\code{double} is the name of the \link{type}.
|
|
|
81 |
\code{numeric} is the name of the \link{mode} and also of the implicit
|
| 50588 |
ripley |
82 |
\link{class}. As an S4 formal class, use \code{"numeric"}.
|
| 43445 |
ripley |
83 |
|
|
|
84 |
The potential confusion is that \R has used \emph{\link{mode}}
|
|
|
85 |
\code{"numeric"} to mean \sQuote{double or integer}, which conflicts
|
|
|
86 |
with the S4 usage. Thus \code{is.numeric} tests the mode, not the
|
|
|
87 |
class, but \code{as.numeric} (which is identical to \code{as.double})
|
|
|
88 |
coerces to the class.
|
|
|
89 |
}
|
| 40667 |
ripley |
90 |
\seealso{
|
| 40923 |
ripley |
91 |
\code{\link{double}}, \code{\link{integer}}, \code{\link{storage.mode}}.
|
| 40667 |
ripley |
92 |
}
|
| 24300 |
ripley |
93 |
\references{
|
| 88581 |
hornik |
94 |
\bibshow{R:Becker+Chambers+Wilks:1988}
|
| 24300 |
ripley |
95 |
}
|
| 502 |
pd |
96 |
\examples{
|
| 78205 |
maechler |
97 |
## Conversion does trim whitespace; non-numeric strings give NA + warning
|
|
|
98 |
as.numeric(c("-.1"," 2.7 ","B"))
|
| 69847 |
maechler |
99 |
|
| 78205 |
maechler |
100 |
## Numeric values are sometimes accidentally converted to factors.
|
|
|
101 |
## Converting them back to numeric is trickier than you'd expect.
|
|
|
102 |
f <- factor(5:10)
|
|
|
103 |
as.numeric(f) # not what you might expect, probably not what you want
|
| 69847 |
maechler |
104 |
## what you typically meant and want:
|
|
|
105 |
as.numeric(as.character(f))
|
| 78205 |
maechler |
106 |
## the same, considerably more efficient (for long vectors):
|
| 69847 |
maechler |
107 |
as.numeric(levels(f))[f]
|
| 502 |
pd |
108 |
}
|
| 286 |
maechler |
109 |
\keyword{classes}
|
|
|
110 |
\keyword{attribute}
|