# Size of Data Frame in R (4 Examples)

In this tutorial, Iâ€™ll explain how to determine the number of rows and columns of a data frame in the R programming language.

Letâ€™s do thisâ€¦

## Creation of Example Data

Have a look at the following example data:

```data <- data.frame(x1 = 10:15,    # Create example data frame
x2 = letters[1:6],
x3 = "l")
data                              # Print example data frame```

As you can see based on Table 1, our example data is a data frame consisting of six observations and three columns. The column x1 has the integer class and the columns x2 and x3 have the character class.

## Example 1: Print Number of Data Frame Rows Using nrow() Function

In Example 1, Iâ€™ll show how to retrieve the number of lines of a data set.

For this task, we can apply the nrow function as shown below:

```nrow(data)                        # Get number of rows
# [1] 6```

As you can see, our data frame contains six rows.

## Example 2: Print Number of Data Frame Columns Using ncol() Function

In Example 2, Iâ€™ll illustrate how to return the number of variables in a data frame.

To accomplish this, we can use the ncol function:

```ncol(data)                        # Get number of columns
# [1] 3```

The RStudio console shows us that our example data frame has three columns.

## Example 3: Print Number of Data Frame Columns Using length() Function

Alternatively to the ncol function, we may also use the length function to get the number of columns of a data frame:

```length(data)                      # Get number of columns
# [1] 3```

The reason why the length function works as well is that data frame objects are basically lists where each list element has the same length. See here for more info.

## Example 4: Print Number of Data Frame Rows & Columns Using dim() Function

The following R programming code demonstrates how to print the number of rows and columns using only one single function, i.e. the dim function.

Consider the R code below:

```dim(data)                         # Get number of rows & columns
# [1] 6 3```

As you can see, the dim function has returned a vector containing the number of rows AND the number of columns.

The dim function is useful when you want to know the size and the dimension of an entire data set.

## Video & Further Resources

I have recently published a video on my YouTube channel, which illustrates how to find the number of observations and variables in a data set based on the examples of this article. You can find the video below:

Furthermore, you might have a look at the related articles which I have published on my website. I have released several related articles already.

To summarize: This article has explained how to get the size of a data object in the R programming language. In case you have any further questions on how to get and handle the dimensions of a data matrix, please let me know in the comments below.

Subscribe to the Statistics Globe Newsletter