# The dim Function in R (4 Examples)

**Basic R Syntax:**

dim(data) |

dim(data)

The dim function of the R programming language **returns the dimension** (e.g. the number of columns and rows) of a matrix, array or data frame. Above, you can see the R code for the application of dim in R.

Continue reading! I’ll provide you with several **example codes and practical tips** in the following article.

## Example 1: Dimension of Matrix or Data Frame

Let’s first create some example data, before we start with the application of dim in R:

set.seed(62626) # Set Seed for reproducibility N <- 500 # Sample size x1 <- round(rnorm(N, 0, 10)) # Create 5 random variables x2 <- round(runif(N, 5, 10)) x3 <- round(runif(N, 1, 3), 1) x4 <- round(runif(N, 10, 20)) x5 <- rpois(N, 5) data <- data.frame(x1, x2, x3, x4, x5) # Data frame with 5 columns head(data) # First 6 rows of data.frame |

set.seed(62626) # Set Seed for reproducibility N <- 500 # Sample size x1 <- round(rnorm(N, 0, 10)) # Create 5 random variables x2 <- round(runif(N, 5, 10)) x3 <- round(runif(N, 1, 3), 1) x4 <- round(runif(N, 10, 20)) x5 <- rpois(N, 5) data <- data.frame(x1, x2, x3, x4, x5) # Data frame with 5 columns head(data) # First 6 rows of data.frame

**Table 1: First 6 Rows of Our Example Data Frame for the Application of dim in R.**

Table 1 illustrates how our example data.frame looks like. It’s easy to see that the data consists of 5 columns. But how many rows? Let’s check with the dim R function:

dim(data) # Apply dim function to data.frame # 500 5 |

dim(data) # Apply dim function to data.frame # 500 5

After applying the dim function in R (I use the RStudio interface), we get two numbers back. The first number reflects the number of rows; and the second number reflects the number of columns.

In other words: Our data frame consists of 500 rows and 5 columns.

The same procedure could be applied to a matrix. Let’s convert our data to the matrix format and check if it works:

data_matrix <- as.matrix(data) # Convert data.frame to matrix dim(data_matrix) # Apply dim function to matrix # 500 5 |

data_matrix <- as.matrix(data) # Convert data.frame to matrix dim(data_matrix) # Apply dim function to matrix # 500 5

Same result as before – perfect!

## Example 2 (Video): dim of a Real Data Frame

In the previous example, I have shown you how to apply dim to a synthetic data set. Do you want to see an example that is based on **more realistic data**? Then you could check out the following video of my YouTube channel *Statistical Programming*. In the video, I’m showing another example for the application of dim to a data frame:

## Example 3: dim of List in R

Sometimes, it is useful to use dim for a list object in R. That task is easily done with a combination of dim() and sapply().

First, let’s create a list in R:

data_list <- list() # Create empty list object data_list[[1]] <- data # First entry of list data_list[[2]] <- data[1:10, ] # Second entry (subset of data) data_list[[3]] <- data[5:67, c(1, 3, 5)] # Third entry (other subset of data) |

data_list <- list() # Create empty list object data_list[[1]] <- data # First entry of list data_list[[2]] <- data[1:10, ] # Second entry (subset of data) data_list[[3]] <- data[5:67, c(1, 3, 5)] # Third entry (other subset of data)

Now, let’s extract the dimensions of each list element:

sapply(data_list, dim) # Get dimension of all list entries # [,1] [,2] [,3] # [1,] 500 10 63 # [2,] 5 5 3 |

sapply(data_list, dim) # Get dimension of all list entries # [,1] [,2] [,3] # [1,] 500 10 63 # [2,] 5 5 3

The combination of dim and sapply returns a matrix to the RStudio console. Each column of this matrix reflects the dimension of one list element:

- List entry 1: 500 rows; 5 columns
- List entry 2: 10 rows; 5 columns
- List entry 3: 63 rows; 3 columns

## Example 4: dim in R Returns NULL – What’s the Problem?

A common mistake is the application of dim to a one dimensional vector or array. Let’s see what happens, when we do this.

I’ll first create an example vector…

vec1 <- c(5, 9, - 20, 3, 17, 18, 2) # Example vector |

vec1 <- c(5, 9, - 20, 3, 17, 18, 2) # Example vector

…and then I’ll apply the dim function:

dim(vec1) # Apply dim function to vector # NULL |

dim(vec1) # Apply dim function to vector # NULL

As you see: That doesn’t work!

In case you want to get the number of entries of a vector, you have to use the length function:

length(vec1) # Get length of vector or array # 7 |

length(vec1) # Get length of vector or array # 7

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R