Get the type of the elements of an array-like object
type.Rd
The S4Arrays package defines a couple of type()
methods to get
the type of the elements of a matrix-like or array-like object.
Arguments
- x
For the default
type()
method: An array-like object. This can be an ordinary array, a SparseArray object from the SparseArray package, a dgCMatrix object from the Matrix package, a DelayedArray object from the DelayedArray package, or any object with an array semantic (i.e. an object for whichdim(x)
is not NULL).For the method for DataFrame objects: A DataFrame derivative for which
as.data.frame(x)
preserves the number of columns. See below for more information.
Details
Note that for an ordinary matrix or array x
, type(x)
is
the same as typeof(x)
.
On an array-like object x
that is not an ordinary array,
type(x)
is semantically equivalent to
typeof(as.array(x))
. However, the actual implementation is
careful to avoid turning the full array-like object x
into
an ordinary array, as this would tend to be very inefficient in general.
For example, doing so on a big DelayedArray object
could easily eat all the memory available on the machine.
On a DataFrame object, type(x)
only
works if as.data.frame(x)
preserves the number of columns,
in which case it is semantically equivalent to
typeof(as.matrix(as.data.frame(x)))
. Here too, the actual
implementation is careful to avoid turning the full object into a
data frame, then into a matrix, for efficiency reasons.
See also
The
type
generic function defined in the BiocGenerics package.SparseArray objects implemented in the SparseArray package.
DelayedArray objects implemented in the DelayedArray package.
DataFrame objects implemented in the S4Vectors package.
Examples
m <- matrix(rpois(54e6, lambda=0.4), ncol=1200)
type(m) # integer
#> [1] "integer"
x1 <- as(m, "dgCMatrix")
type(x1) # double
#> [1] "double"
library(SparseArray)
x2 <- SparseArray(m)
type(x2) # integer
#> [1] "integer"