Retrieving data frame dimensions

nrow(___)
ncol(___)
dim(___)
length(___)
View Interactive Version

To retrieve the size of all dimensions from a data frame at once you can use the dim() function. dim() returns a vector with two elements, the first element is the number of rows and the second element the number of columns.

For example, the dimensions of the Davis dataset can be retrieved as

Input
dim(Davis)
Output
[1] 200   5

In addition to data frames dim() can also be used for other multi-dimensional R objects such as matrices or arrays. However, when used with vectors dim only returns NULL:

Input
dim(c(1, 3, 5, 7))
Output
NULL

Instead, the length of a vector is determined through length():

Input
length(c(1, 3, 5, 7))
Output
[1] 4

In the case of a data frame length() returns its number of columns:

Input
length(Davis)
Output
[1] 5
Determine the size of a data frame