R dim Function


dim() function gets or sets the dimension of a matrix, array or data frame.

dim(x)

x: array, matrix or data frame.

>BOD #R Biochemical Oxygen Demand Dataset
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

>class(BOD)
[1] "data.frame"

>dim(BOD) #get dimension
[1] 6 2

Set dimension of a matrix:
>x <- rep(1:20)
>x
 [1]  1  2  3  4  5  6  7  8  9 10

Set dimension to 2 × 5:
>dim(x) <- c(2,5)
>x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10