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()
andncol()
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.
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.
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.
- If the input is a matrix, the output will be equal to the number of entries in the matrix.