**dim()** function is used to get the dimension of a matrix, array, data frame, or vector. It is a part of the base R package. This guide explains how to use the dim() in R.

## Syntax

`dim(x)`

## Arguments

**x**= a vector, matrix, array, or data frame

## Using dim() to get the dimension of a Vector

A vector is a one-dimensional data structure. To get the dimension of a vector, you need to specify the vector as an argument to the dim() function. It will always return **NULL**.

#### Example

```
#creating a vector
x <- 1:3
#printing vector
x
#getting the dimension of a vector
dim(x)
```

#### Output

```
[1] 1 2 3
NULL
```

## Using dim() to get the dimension of a Matrix

A matrix is a two-dimensional array. To get the dimension of a matrix, you need to specify the matrix as an argument to the dim() function. It will return a vector of **two integers** that indicate **the number of rows and columns in the matrix**.

#### Example

```
#creating a matrix
y <- matrix(1:9, nrow=3, ncol=3)
#printing matrix
y
#getting the dimension of a matrix
dim(y)
```

#### Output

```
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] 3 3
```

## Using dim() to get the dimension of an Array

An array is a three-dimensional data structure. To get the dimension of an array, you need to specify the array as an argument to the dim() function. It will return a vector of **three integers** that indicate the **size of the array**.

#### Example

```
#creating an array
a <- array(1:8, c(2, 2, 2))
#printing array
a
#getting the dimension of the array
dim(a)
```

#### Output

```
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
[1] 2 2 2
```

## Using dim() to get the dimension of a Data Frame

A data frame is a two-dimensional data structure. To get the dimension of a data frame, you need to specify the data frame as an argument to the dim() function. It will return a vector of **two integers** that indicate **the number of rows and columns in the data frame**.

#### Example

```
#creating a data frame
df <- data.frame(x=1:3, y=4:6)
#printing data frame
df
#getting the dimension of the data frame
dim(df)
```

#### Output

```
x y
1 1 4
2 2 5
3 3 6
[1] 3 2
```

Here, 3 indicates the number of rows in the data frame, and 2 indicates the number of columns.