integer
Integer Objects
Description
Creates or tests for objects with storage mode integer.
Usage
integer(length = 0L)
is.integer(x)
as.integer(x, ...)
Arguments
x |
any object.
|
length |
an integer giving the length of the returned object.
|
... |
other arguments to pass to or from other internal functions.
|
Details
Both as.integer and is.integer are methods of generic functions.
Note the difference between coercing to a simple object
of storage mode
"integer" and setting the storage mode attribute:
storage.mode(myobject) <- "integer"
Just setting the storage mode attribute changes the storage mode of
myobject, but it leaves all other attributes unchanged
(so a matrix stays a matrix). The value of
as.integer(myobject)
has no attributes.
Value
integer | returns a simple object of storage mode
"integer", and the length specified. |
is.integer | returns TRUE if x is an object
of storage mode "integer". Othwerwise, it returns FALSE. |
as.integer | returns x if x is a simple object
of storage mode "integer". Otherwise, it returns an integer
object of the same length as x and with data resulting
from coercing the elements of x to storage mode "integer".
The numbers are truncated (moved to the closest integer of the original
number that is towards zero). Attributes are deleted. |
if
x can not be coerced to mode
"integer",
NAs will be introduced.
Background
In most expressions, it is not necessary to ensure explicitly that data
are of a particular storage mode. For example, a numeric subscript vector
need not be integer; it will be coerced to integer as needed. When testing
for data suitable for arithmetic, for example, it is better to use
is.numeric(x), which returns TRUE for any numeric object.
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
z <- integer(5)
x<-1:3
is.integer(x)
# [1] TRUE
y<-1
storage.mode(y)
# [1] "double"
is.integer(y)
# [1] FALSE
y1 <- as.integer(y)
is.integer(y1)
# [1] TRUE
x<-c("1","a")
as.integer(x)
#Warning message: NAs introduced by coercion
# [1] 1 NA