Creates or coerces objects of type
is.numeric is a more general test of an object being
interpretable as numbers.
numeric(length = 0) as.numeric(x, ...) is.numeric(x)
- 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.
- object to be coerced or tested.
- further arguments passed to or from other methods.
numeric is identical to
It creates a double-precision vector of the specified length with each
element equal to
as.numeric is a generic function, but S3 methods must be
as.double. It is identical to
is.numeric is an internal generic
function: you can write methods to handle specific classes of objects,
see InternalMethods. It is not the same as
is.double. Factors are handled by the default method,
and there are methods for classes
"difftime" (all of which
return false). Methods for
is.numeric should only return true
if the base type of the class is
and values can reasonably be regarded as numeric
(e.g., arithmetic on them makes sense, and comparison should be done
via the base type).
for numeric and as.numeric see double.The default method for is.numeric returns TRUE
if its argument is of mode "numeric"
(type "double" or type "integer") and not a
factor, and FALSE otherwise. That is,
is.integer(x) || is.double(x), or
(mode(x) == "numeric") && !is.factor(x).
x is a
as.numeric will return
the underlying numeric (integer) representation, which is often
meaningless as it may not correspond to the
levels, see the Warning section in
factor (and the 2nd example below).
is.numeric are internally S4 generic and
so methods can be set for them via
setMethod. To ensure that
remain identical, S4 methods can only be set for
Note on names
It is a historical anomaly that R has two names for its
(and formerly had
double is the name of the type.
numeric is the name of the mode and also of the implicit
class. As an S4 formal class, use
"numeric". The potential confusion is that R has used mode
"numeric" to mean double or integer, which conflicts
with the S4 usage. Thus
is.numeric tests the mode, not the
as.numeric (which is identical to
coerces to the class.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
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]