dim(x) dim(x) <- value
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. |
dim(x) | returns an integer vector of the dimensions of x, if any. It returns NULL if x does not have dimensions. |
dim(x) <- value | returns value. |
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 12dim(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