| 42333 |
ripley |
1 |
% File src/library/base/man/numeric.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 61810 |
ripley |
3 |
% Copyright 1995-2013 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{
|
| 56156 |
ripley |
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{
|
| 41620 |
ripley |
31 |
\code{numeric} is identical to \code{\link{double}} (and \code{real}).
|
|
|
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,
|
|
|
42 |
and there are methods for classes \code{"\link{Date}"},
|
|
|
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
|
|
|
47 |
(e.g. arithmetic on them makes sense, and comparison should be done
|
|
|
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 |
}
|
| 41620 |
ripley |
60 |
\section{S4 methods}{
|
|
|
61 |
\code{as.numeric} and \code{is.numeric} are internally S4 generic and
|
|
|
62 |
so methods can be set for them \emph{via} \code{setMethod}.
|
| 25376 |
ripley |
63 |
|
| 61812 |
ripley |
64 |
To ensure that \code{as.numeric} and \code{as.double}
|
| 41620 |
ripley |
65 |
remain identical, S4 methods can only be set for \code{as.numeric}.
|
| 2 |
r |
66 |
}
|
| 43445 |
ripley |
67 |
%% keep next the same in double.Rd & numeric.Rd
|
|
|
68 |
\section{Note on names}{
|
| 61810 |
ripley |
69 |
It is a historical anomaly that \R has two names for its
|
|
|
70 |
floating-point vectors, \code{\link{double}} and \code{\link{numeric}}
|
|
|
71 |
(and formerly had \code{real}).
|
| 43445 |
ripley |
72 |
|
|
|
73 |
\code{double} is the name of the \link{type}.
|
|
|
74 |
\code{numeric} is the name of the \link{mode} and also of the implicit
|
| 50588 |
ripley |
75 |
\link{class}. As an S4 formal class, use \code{"numeric"}.
|
| 43445 |
ripley |
76 |
|
|
|
77 |
The potential confusion is that \R has used \emph{\link{mode}}
|
|
|
78 |
\code{"numeric"} to mean \sQuote{double or integer}, which conflicts
|
|
|
79 |
with the S4 usage. Thus \code{is.numeric} tests the mode, not the
|
|
|
80 |
class, but \code{as.numeric} (which is identical to \code{as.double})
|
|
|
81 |
coerces to the class.
|
|
|
82 |
}
|
| 40667 |
ripley |
83 |
\seealso{
|
| 40923 |
ripley |
84 |
\code{\link{double}}, \code{\link{integer}}, \code{\link{storage.mode}}.
|
| 40667 |
ripley |
85 |
}
|
| 24300 |
ripley |
86 |
\references{
|
|
|
87 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
88 |
\emph{The New S Language}.
|
| 47262 |
ripley |
89 |
Wadsworth & Brooks/Cole.
|
| 24300 |
ripley |
90 |
}
|
| 502 |
pd |
91 |
\examples{
|
| 5219 |
pd |
92 |
as.numeric(c("-.1"," 2.7 ","B")) # (-0.1, 2.7, NA) + warning
|
| 502 |
pd |
93 |
as.numeric(factor(5:10))
|
|
|
94 |
}
|
| 286 |
maechler |
95 |
\keyword{classes}
|
|
|
96 |
\keyword{attribute}
|