numeric
Numeric Objects
Description
Creates or tests for objects of mode numeric.
Usage
numeric(length = 0L)
as.numeric(x, ...)
is.numeric(x)
## S3 method for class 'Date':
is.numeric(x)
## S3 method for class 'POSIXt':
is.numeric(x)
## S3 method for class 'difftime':
is.numeric(x)
Arguments
x |
any object.
|
... |
other arguments to pass to or from future functions.
|
length |
an integer giving the length of the returned object.
|
Details
If you want a factor to be treated as integer, use as.integer.
Coercing to a simple object of mode "numeric" is different from
setting the mode attribute using
mode(myobject) <- "numeric". While setting the mode
attribute changes the mode of myobject, it leaves all other
attributes unchanged (so a matrix stays a matrix, for example).
The value of as.numeric(myobject) has no attributes.
Value
numeric | returns vector of a simple object of mode "numeric" and
the length specified. The storage mode is "double". If length
is greater than 0, the values in this vector are 0.
|
is.numeric |
returns FALSE if x is an object of class Date, POSIXt, or difftime.
Otherwise, it returns TRUE if x has mode "numeric" and FALSE if it does not.
Its behavior is
unaffected by any attributes of x. For example, x could
be a numeric array (in contrast to the behavior of is.vector).
|
as.numeric | returns a vector like x, but with storage
mode "double", if x is a simple object of mode
"numeric". Otherwise, as.numeric returns a numeric object
of the same length as x and with data resulting from coercing
the elements of x to mode "numeric".
|
if
x can not be coerced to mode
"numeric",
NAs are introduced.
Background
When x is of mode "numeric", the data of x can
be stored as integers or double precision floating point numbers,
and storage.mode(x) is "integer" or "double",
respectively.
Normally, all numeric constants that appear in expressions
are read with mode numeric and storage mode double.
This distinction is relevant only when you use the
interface to languages like C or Fortran.
The class of simple objects has no attributes. In most expressions,
it is not necessary to ensure explicitly that data
are of a particular mode.
References
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software.
See Also
Examples
x <- numeric(5)
y <- c(1, 2, "a")
is.numeric(y)
# [1] FALSE
as.numeric(y)
# [1] 1 2 NA
# Warning message: NAs introduced by coercion
z <- matrix(1:20, nrow = 4)
is.numeric(z)
# [1] TRUE
as.numeric(z)
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
mode(z) <- "numeric" # still a matrix
is.numeric(Sys.Date())
is.numeric(Sys.time())
is.numeric(Sys.time() - as.POSIXct(Sys.Date()))