40 length, rows, columns and dimensions

Written by Isaac Ehrlich and last updated on 7 October 2021.*

40.1 Introduction

Understanding the size and dimensions of your variables can be important in a variety of contexts.

In this lesson, you will learn how to

  • Use length() to find the length of vectors
  • Use nrow() and ncol() to find the dimensions of matrices and data frames
  • Use dim() to find and set dimensions

Prerequisites:

  • Understanding how to create vectors and data frames and their basic principles

40.2 Arguments

40.2.1 length()

length() returns the length (number of items) in a vector. The only argument that length() takes is the vector.

x <- c("a", "b", "c", "d")
length(x)
#> [1] 4

40.2.2 nrow() and ncol()

nrow() and ncol() return the number of rows and columns of a matrix or data frame, respectively. The only argument that nrow() and ncol() take is the matrix or data frame.

df <- data.frame(col1 = c("a", "b", "c", "d"), col2 = c(1,2,3,4))
df
#>   col1 col2
#> 1    a    1
#> 2    b    2
#> 3    c    3
#> 4    d    4

# Number of Rows:
nrow(df)
#> [1] 4

# Number of Columns:
ncol(df)
#> [1] 2

40.2.3 dim()

dim() returns a vector with two elements that denote the dimensions of a matrix or data frame, in the order ‘number of rows,’ ‘number of columns.’ The first element is equivalent to the output of nrow() and the second to that of ncol(). Similarly to nrow() and ncol(), the only argument that dim() takes is the matrix or data frame.

# Dimensions of data frame:
dim(df)
#> [1] 4 2

# Note the relationship between 'dim()', 'nrow()', and 'ncol()'
dim(df)[1] == nrow(df)
#> [1] TRUE
dim(df)[2] == ncol(df)
#> [1] TRUE

Unlike nrow() and ncol() however, dim() can also be used to set the dimensions of a vector or matrix. Setting dimensions for a vector will turn it into a matrix.

# Create a vector
x <- c("a", "b", "c", "d")
# Check dimensions of x - should output NULL since vectors do not have dimensions
dim (x)
#> NULL

# Set and check new dimensions
dim(x) <- c(2, 2)
dim(x)
#> [1] 2 2

# Output 'x'
x
#>      [,1] [,2]
#> [1,] "a"  "c" 
#> [2,] "b"  "d"

Setting new dimensions for an existing matrix will reshape it.

# Create a matrix and check its dimensions
m <- matrix(1:12)
dim(m)
#> [1] 12  1

# Set and check new dimensions
dim(m) <- c(3,4)
dim(m)
#> [1] 3 4

40.2.4 dplyr::bind_rows() and dplyr::bind_cols()

Finally, it is worth mentioning that there are also dplyr versions of rbind() and cbind(). dplyr::bind_rows() is actually a bit more robust since it doesn’t require the input data frames to have the same number of columns. If you are making extensive use of rbind() and cbind(), then exploring their dplyr counterparts would be worthwhile.

40.3 Questions and Exercises

40.3.1 length()

For questions about length() we will be using the built in vector fruit, a vector of fruit names.

1.

40.3.2 nrow() and ncol()

For questions about nrow() and ncol() we will be using the data frame starwars, which contains Star Wars characters and information about them, and a secret matrix.

1. Save the number of rows and number of columns in starwars

2. I’ve created a matrix, secret_matrix, the dimensions of which are a secret. Without printing out the number of rows and columns, check to see if secret_matrix is a square matrix (i.e. it has the same number of rows and columns)

3. Using indexing, nrow(), and ncol(), in one line, save the value in the bottom right corner of the matrix secret_matrix.

40.3.3 dim()

2. Use dim() to turn the fruit vector into a matrix with 8 rows and 10 columns

40.4 Special Cases & Common Mistakes

40.4.1 length()

  • Remember that length() returns the number of objects in a vector, not the number of digits or characters in a string. See examples below.
length("This sentence has 31 characters")
#> [1] 1
length(c("This sentence has 31 characters", "This sentence has 7 less"))
#> [1] 2
  • Although a vector is the correct input into length(), if the input is a dataframe, the output will be equal to the number of columns.
length(starwars) == ncol(starwars)
#> [1] TRUE
  • If the input is a matrix, the output will be equal to the number of entries in the matrix.
length(secret_matrix) == nrow(secret_matrix) * ncol(secret_matrix)
#> [1] TRUE

40.4.2 nrow() and ncol()

# Check dimensions of 'letters', a built-in vector of English letters
nrow(letters)
#> NULL
ncol(letters)
#> NULL
dim(letters)
#> NULL

40.5 Overview and Next Steps

length(), nrow(), and ncol(), are useful functions for finding the dimensions of variables. In the next section, we will continue exploring data frames and how to manipulate them.

40.6 Exercises

40.6.1 Question 1

40.6.2 Question 2

40.6.3 Question 3

40.6.4 Question 4

40.6.5 Question 5

40.6.6 Question 6

40.6.7 Question 7

40.6.8 Question 8

40.6.9 Question 9

40.6.10 Question 10