dim
Dimensions of an Object
Retrieve or set the dimension of an object.
- Keywords
- array
Usage
dim(x)
dim(x) <- value
Arguments
- x
- an R object, for example a matrix, array or data frame.
- value
- For the default method, either
NULL
or a numeric vector, which is coerced to integer (by truncation).
Details
The functions dim
and dim<-
are internal generic
primitive functions.
dim
has a method for data.frame
s, which returns
the lengths of the row.names
attribute of x
and
of x
(as the numbers of rows and columns respectively).
Value
-
For an array (and hence in particular, for a matrix) dim retrieves
the dim attribute of the object. It is NULL or a vector
of mode integer.The replacement method changes the "dim" attribute (provided the
new value is compatible) and removes any "dimnames" and
"names" attributes.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
See Also
Examples
library(base)
x <- 1:12 ; dim(x) <- c(3,4)
x
# simple versions of nrow and ncol could be defined as follows
nrow0 <- function(x) dim(x)[1]
ncol0 <- function(x) dim(x)[2]
Community examples
For matrices, `dim()` returns the number of rows and columns as an integer vector. ```{r} m <- matrix(1:12, 3, 4) dim(m) # 3 4 ``` The same is true for data frames. ```{r} d <- data.frame(a = 1:3, b = 4:6, c = 7:9, d = 10:12) dim(d) # 3 4 ``` For arrays, `dim()` can be longer than 2 values. ```{r} a <- array(1:24, dim = 2:4) dim(a) # 2 3 4 ``` Vectors don't have a dimension attribute; `dim()` will always return `NULL`. ```{r} v <- 1:12 dim(v) # NULL ```