What is the size of a data frame in R?

What is the size of a data frame in R?

A data frame in R is a two-dimensional structure that is used to store data in a tabular format. The size of a data frame refers to the total number of rows and columns it contains. It is often denoted as “n x m”, where “n” represents the number of rows and “m” represents the number of columns. The size of a data frame can vary depending on the amount of data it contains, but it can typically handle thousands of rows and hundreds of columns. The size of a data frame can be determined using the “nrow()” and “ncol()” functions in R.

Find the Size of a Data Frame in R


You can use the following functions in R to display the size of a given data frame:

  • nrow: Display number of rows in data frame
  • ncol: Display number of columns in data frame
  • dim: Display dimensions (rows and columns) of data frame

The following examples show how to use each of these functions in practice with the following data frame:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F'),
                 points=c(99, 90, 86, 88, 95, 99),
                 assists=c(33, 28, 31, 39, 34, 25),
                 rebounds=c(12, NA, 24, 24, 28, 33))

#view data frame
df

  team points assists rebounds
1    A     99      33       12
2    B     90      28       NA
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28
6    F     99      25       33

Example 1: Use nrow() to Display Number of Rows

The following code shows how to use the nrow() function to display the total number of rows in the data frame:

#display total number of rows in data frame
nrow(df)

[1] 6

There are 6 total rows.

Note that we can also use the function to display the total number of rows with no NA values:

#display total number of rows in data frame with no NA values
nrow(df[complete.cases(df), ])

[1] 5

There are 5 total rows that have no NA values.

Example 2: Use ncol() to Display Number of Columns

The following code shows how to use the ncol() function to display the total number of columns in the data frame:

#display total number of columns in data frame
ncol(df)

[1] 4

There are 4 total columns.

Example 3: Use dim() to Display Dimensions

The following code shows how to use the dim() function to display the dimensions (rows and columns) of the data frame:

#display dimensions of data frame
dim(df)

[1] 6 4

This tells us there are 6 rows and 4 columns in the data frame.

You can also use brackets with the dim() function to display only the rows or columns:

#display number of rows of data frame
dim(df)[1]

[1] 6

#display number of columns of data frame
dim(df)[2]

[1] 4

Cite this article

stats writer (2024). What is the size of a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-size-of-a-data-frame-in-r/

stats writer. "What is the size of a data frame in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/what-is-the-size-of-a-data-frame-in-r/.

stats writer. "What is the size of a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-size-of-a-data-frame-in-r/.

stats writer (2024) 'What is the size of a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-size-of-a-data-frame-in-r/.

[1] stats writer, "What is the size of a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. What is the size of a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top