Determine the size of a data frame

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The size of a data frame, like the number of rows or columns, is often required and can be determined in various ways.

  • Get number of rows of a data frame
  • Get number of columns of a data frame
  • Get dimensions of a data frame
nrow(___)
ncol(___)
dim(___)
length(___)

Data Frame Dimensions

nrow(___)
ncol(___)
dim(___)
length(___)

The number of rows and columns in a data frame can be guessed through the printed output of the data frame. However, it is much easier to get this information directly through functions. Additionally, you might want to use this information in some parts of the code.

Data frames have two dimensions. The number of rows is considered to be the first dimension. It typically defines the number of observations in a data set. To get the number of rows from the Davis data frame in the carData dataset use the nrow() function:

nrow(Davis)
[1] 200

Similarly, the number of columns or attributes of the data frame can be retrieved with ncol():

ncol(Davis)
[1] 5

Exercise: Determine number of elements in data frame

                              survived    sex age passengerClass
Allen, Miss. Elisabeth Walton      yes female  29            1st
 [ reached 'max' / getOption("max.print") -- omitted 1308 rows ]

Determine the number of data values in the TitanicSurvival data frame above given as the number of rows multiplied by the number of columns.

Start Exercise

Retrieving data frame dimensions

nrow(___)
ncol(___)
dim(___)
length(___)

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

dim(Davis)
[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:

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

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

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

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

length(Davis)
[1] 5

Quiz: Data Frame Dimensions

dim(Florida)
What does the above command return for the data set Florida from the carData package which has 11 columns and 67 rows?
  • 67 11
  • 11 67
  • 11
  • 67
Start Quiz

Determine the size of a data frame is an excerpt from the course Introduction to R, which is available for free at quantargo.com

VIEW FULL COURSE

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)