Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 10

Exploring DataFrame Structure

Recommended resources

Hello! Welcome to the next step in your R programming journey.

Introduction

In our last lesson, you mastered the essential skill of importing data into R from common research file formats like CSV, Excel, and SPSS. You now have several data frames loaded into your R environment, ready for analysis. But before diving into statistical tests or plotting, a crucial, non-negotiable step is to get acquainted with your data. You must inspect its structure, check its contents, and understand its basic characteristics.

This lesson focuses on exactly that: examining the structure of a data frame. You will learn to use three fundamental functions—str(), glimpse(), and summary()—that form the backbone of initial data exploration. These tools allow you to quickly answer questions like: How many participants are in my dataset? What variables were measured? What are the data types? Are there any missing values? Mastering this initial "meet and greet" with your data is the foundation of any sound analysis.


1. A First Look: head() and tail()

Before dissecting the structure, the simplest way to get a feel for a dataset is to look at the raw data itself. Does it look like what you expected? The head() and tail() functions are perfect for this. They allow you to view the first and last few rows of your data frame, respectively. By default, they show six rows.

Chapter 5 Importing Your Data into R

This reading from "Reproducible Medical Research with R" provides a concise introduction to using head() and tail() to preview your data.

In the text, please find and read the subsection on head() and tail(). Pay attention to the simple syntax and the output they produce.

In your R console, you can try this with the penguins_data object you created in the last lesson:




# View the first 6 rows
head(penguins_data)




# View the last 6 rows
tail(penguins_data)

This is a great first-pass check to ensure your data imported correctly and the columns are what you expect.


2. The Blueprint of Your Data: str() and glimpse()

While head() shows you the data, it doesn't tell you about the underlying structure. For that, we turn to str() and glimpse().

str() - The Classic Structure Function

The str() function is a versatile tool built into base R. It provides a compact, text-based summary of any R object's internal structure. When used on a data frame, it gives you a wealth of information at once.

The DataCamp video below gives a clear, quick overview of str().

R tutorial: Exploring raw data

This short clip introduces the str() function and explains what each part of its output means.

About a minute and a half in, please watch the segment on the str function. Note how it displays the dimensions (observations and variables), column names, data types (e.g., integer, numeric), and a preview of the data.

As the video explained, the output of str() shows you:

  • The object's class (e.g., 'data.frame' or 'tbl_df').
  • The dimensions: number of observations (rows) and variables (columns).
  • For each variable (column):
    • Its name (e.g., bill_length_mm).
    • Its data type (e.g., num for numeric, chr for character, fct for factor).
    • The first few data points.

glimpse() - The Tidyverse Alternative

The tidyverse suite of packages, which you'll be using extensively, offers its own version of str() called glimpse(). The goal is the same, but the output is often cleaner and more readable, designed to fit neatly in your console.

Explore your data using R programming

This video from R Programming 101 effectively contrasts the output of str() with glimpse(), showing why glimpse() is often preferred in modern R workflows.

Watch the portion of the video starting around two minutes and forty seconds where the presenter first uses the str() function on a messy dataset and then demonstrates the much neater output from the glimpse() function.

As you saw, glimpse() transposes the output, listing each variable on its own line. This makes it very easy to scan down the list of variables, their types, and their first few values. For your work, glimpse() will likely become your default choice for structural inspection.

Let's refer back to the "Reproducible Medical Research with R" text for a side-by-side comparison.

Chapter 5 Importing Your Data into R

These sections walk through examples of both glimpse() and str(), reinforcing what you saw in the videos.

First, read section 5.5.1 on the glimpse() function. Then, read section 5.5.4 on the str() function to see how it provides similar information with slightly different formatting and detail.


3. A Statistical Overview: summary()

Once you understand the structure of your data, the next step is to get a quantitative summary. The summary() function does exactly this. Unlike str() and glimpse(), which focus on data types and structure, summary() provides descriptive statistics.

Critically, the output of summary() depends on the data type of the column.

  • For numeric columns (like age or blood pressure), it calculates the minimum, 1st quartile, median, mean, 3rd quartile, and maximum. This "five-number summary" (plus the mean) is a cornerstone of descriptive statistics and gives you an instant sense of the variable's distribution and central tendency.
  • For character or factor columns (like treatment group or gender), it provides a frequency table, showing how many observations fall into each category.
  • For all columns, it also reports the number of missing values, shown as NA's. This is one of the most important pieces of information you can get, as it will guide your data cleaning process.
This screenshot shows R code on the left creating a data frame `df_week`. On the right, in the console, you can see the output of `summary(df_week)`. Notice how it provides the `Length`, `Class`, and `Mode` for the character vectors `day` and `class_period`, which is how `summary()` handles non-numeric data.

The following video provides a great explanation of this behavior.

R tutorial: Exploring raw data

This clip explains how the summary() function works and how its output differs for numeric and non-numeric data.

About three minutes in, watch the segment on the summary function. The key takeaway is how the output intelligently adapts to the type of data in each column.

Now, let's see an example of summary() applied to a larger, mixed-data-type data frame.

Practice with R - Introduction to R and Basic Data Analysis

This resource shows the output of the summary() function on a complete data frame with various data types, giving you a clear picture of what to expect in a real-world scenario.

Please find the heading "Mutate and Merge Data" and scroll down to the code chunk that runs claims_data_raw %>% summary(). Examine the output carefully. See how it provides statistical summaries for numeric columns like ClaimAmount and frequency counts for factor-like columns such as Brand and Region.


Let's Practice

It's time to apply these inspection tools to the datasets you imported in the last lesson. Please continue in your clinical-trial-practice RStudio Project. You can create a new R Script named 03-data-exploration.R.

  1. Load packages. As always, start by loading the tidyverse package (which includes glimpse) and haven (to ensure the SPSS data displays correctly).

    library(tidyverse)
    library(haven)
    
    
    
    
    # Re-run the import code from the last lesson if the objects are not in your Environment
    penguins_url <- "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/inst/extdata/penguins.csv"
    penguins_data <- read_csv(penguins_url)
    
    tb_url <- "https://raw.githubusercontent.com/higgi13425/rmrwr-book/master/data/strep_tb.sav"
    tb_data <- read_sav(tb_url)
    
  2. Task 1: Inspect the penguins_data dataset. This is a clean, standard dataset.

    • Run glimpse(penguins_data). Note the different data types: chr (character), dbl (double, a type of numeric), and int (integer).
    • Run summary(penguins_data).
      • Look at the summary for bill_length_mm. What are the mean and median bill lengths?
      • Look at the summary for species. How many of each penguin species are in the dataset?
      • How many missing values (NA's) are in the bill_length_mm column?
  3. Task 2: Inspect the tb_data (SPSS) dataset. This dataset contains special labels from SPSS.

    • Run str(tb_data).
    • Now run glimpse(tb_data). Compare the output. You should find glimpse() much easier to read.
    • Notice the <S3: labelled> data type for many columns. This is how haven preserves the value labels from SPSS (e.g., where 1 is labeled "Male"). This is extremely useful metadata.
    • Run summary(tb_data). Which variable has a single missing value?

By running these commands, you've performed the first essential steps of exploring a new dataset, a routine you will repeat every time you start a new analysis.


Conclusion

You have now learned how to perform an initial investigation of any data frame you encounter. This is a fundamental skill that moves you from simply importing data to truly understanding it.

Key Takeaways:

  • head() and tail() provide a quick, sanity-check view of the first and last rows of your data.
  • str() is the base R function for displaying the object's structure, including dimensions, variable names, and data types.
  • glimpse() is the tidyverse-aligned alternative to str(), offering a more readable, column-by-column structural view. It will likely be your preferred choice.
  • summary() provides a statistical overview, intelligently adapting its output: descriptive statistics for numeric data and frequency counts for categorical data. It is your primary tool for getting a quantitative feel for the data and, crucially, for identifying missing values (NAs).

Preview of the Next Lesson:
During your inspection with glimpse() and str(), you saw variables classified as character (like species in the penguins dataset) and factor. In R, categorical data is formally represented using factors. Understanding the difference and knowing how to convert between these types is a critical data cleaning skill. In the next lesson, we will differentiate between and convert factors and character vectors, giving you more control over how R interprets your categorical variables for analysis and plotting.