How Do You Find the Structure of a Data Frame in R?


Larry Thompson

How Do You Find the Structure of a Data Frame in R?

When working with data frames in R, it’s important to understand their structure. The structure of a data frame provides information about the variables (columns) and observations (rows) present in the data. It helps you identify the type of each variable, check for missing values, and get an overall understanding of the dataset.

Using the str() function

The easiest way to find the structure of a data frame in R is by using the str() function. This function provides a concise summary of an object, including its structure, dimension, and variable types.

To demonstrate this, let’s consider a sample data frame called my_data:

my_data <- data.frame(
  name = c("John", "Jane", "Mike"),
  age = c(25, 30, 35),
  city = c("New York", "London", "Paris"),
  married = c(TRUE, FALSE, TRUE)

To find the structure of my_data, simply call str(my_data):


The output will be:

'data.frame':	3 obs. of  4 variables:
 $ name   : Factor w/ 3 levels "Jane","John","Mike": 2 1 3
 $ age    : num  25 30 35
 $ city   : Factor w/ 3 levels "London","New York",..: 2 1 3
 $ married: logi  TRUE FALSE TRUE

Analyzing the Output

The output from str() provides valuable information about the data frame:

  • 'data.frame': Indicates the type of object, which in this case is a data frame.
  • 3 obs.: The number of observations or rows in the data frame.
  • 4 variables: The number of variables or columns in the data frame.

Each variable is described with:

  • $ name: The name of the variable.
  • : Factor w/ 3 levels: Indicates that the variable is a factor with three levels (categories).
  • $ age: The name of the second variable.
  • : num: Indicates that the variable is numeric (continuous).
  • $ city: The name of the third variable.
  • $ married: The name of the fourth variable.
  • : logi: Indicates that the variable is logical (boolean).

Incorporating Additional Functions for Detailed Analysis

Besides using str(), there are other helpful functions to further analyze the structure and properties of a data frame:


The summary() function provides summary statistics for each column, such as minimum, maximum, median, and quartiles. It also gives you an overview of factor variables by displaying the frequency of each level.



The head() function allows you to view the first few rows of a data frame. This is useful when you want to get a quick glimpse of the data.



The names() function returns the names of all variables in a data frame.



The dim() function gives you the dimensions (number of rows and columns) of a data frame. It is particularly useful when dealing with larger datasets.


In Conclusion

In this tutorial, we learned how to find the structure of a data frame in R using the str() function. We also explored additional functions like summary(), head(), names(), and dim(), which can provide more detailed information about the dataset.

Understanding the structure of a data frame is crucial for any data analysis task in R. It allows you to make informed decisions about data manipulation, visualization, and modeling. So, make sure to always explore and analyze your datasets using these handy functions!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy