dim
Dim Attribute of an Object

Description

Returns or changes the dim attribute, which describes the dimensions of a matrix, a data frame, or an array.

Usage

dim(x) 
dim(x) <- value

Arguments

x any object.
value must be a vector of integers (or anything that can be coerced to a vecter of integers). The product of value should be equal to the length of x. Missing values or negative integers are not allowed. If NULL, the dim attribute is removed.

Details

This is a generic function. dim has a method for data frames that counts the length of the row.names attribute and the number of columns. You cannot change the dimensions of a data frame.
Arrays and matrices have a dim attribute that is an integer vector, with the property that prod(dim(x))==length(x). This attribute identifies an object as an array.
Value
dim(x)returns an integer vector of the dimensions of x, if any. It returns NULL if x does not have dimensions.
dim(x) <- valuereturns value.
Side Effects
Adding or removing the "dim" attribute typically affects the class of x.
Assigning dim to a vector, a matrix, or an array removes the names and dimnames attributes. (No re-ordering of the elements of x is implied.)
See Also
dimnames, ncol, array, matrix, data.frame, length.
Examples
x <- matrix(1:12, nrow = 3)
dim(x)  #   c(3, 4)
length(dim(x)) # 2
x
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12

dim(x) <- c(2, 6) class(x) # matrix x # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] 1 3 5 7 9 11 # [2,] 2 4 6 8 10 12

# dim(x) <- c(2, 5) # Error in dim(x) <- c(2, 5) : # dims [product 10] do not match the length of object [12]

dim(x) <- c(2, 3, 2) class(x) # array length(dim(x)) # 3 x # , , 1 # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 2 4 6 # , , 2 # [,1] [,2] [,3] # [1,] 7 9 11 # [2,] 8 10 12

dim(x) <- NULL class(x) # integer x # [1] 1 2 3 4 5 6 7 8 9 10 11 12

Package base version 6.1.4-13
Package Index