Skip to main content

5  R Objects, Data Frames, and Debugging

5.1 Why This Week Matters

In Module 01, you saw the whole workflow. In Module 02, we slow down and ask what R is actually working with.

Most beginner errors are not caused by a lack of intelligence. They happen because the analyst has not yet checked the object, the object type, the column name, or the exact error message. This week teaches you to diagnose before you guess.

Debugging is not a separate skill from data analysis. Debugging is how data analysis stays honest.

5.2 Learning Goals

By the end of this week, you should be able to:

  • explain what an R object is
  • distinguish a vector from a data frame
  • use class(), str(), names(), and summary() to inspect an object
  • identify rows and columns in a data frame
  • read an error message before changing code
  • fix a small bug by changing the cause, not by hiding the symptom

5.3 Vocabulary

Term Meaning in this course Example
object A named thing stored in R my_values
vector A one-dimensional collection of values c(10, 20, 40)
data frame A table with rows and columns soil_samples
row One observation or record in a data frame one soil sample
column One variable in a data frame ph
class The type of an object numeric, character, data frame
character Text data "sandy"
numeric Number data R can use for arithmetic 6.5
missing value A value that is absent or unknown NA
error A message that says R could not complete the command object not found

5.4 Before You Start

Module 02 uses:

  • materials/week_02/week_02.Rproj
  • materials/week_02/scripts/week_02_follow_along.R
  • materials/week_02/data/soil_samples_messy.csv

The soil data are intentionally small and imperfect:

site_id,ph,sample_date,texture
1,6.5,2026-05-19,sandy
2,7.2,2026-05-20,loam
3,NA,2026-05-21,clay
4,5.8,2026-05-22,sandy
5,"7,0",2026-05-23,loam
6,6.1,unknown,clay

This dataset includes problems you will see in real data:

  • a missing pH value
  • a pH value written with a comma instead of a decimal point
  • a date field with the word unknown

At this point, you are not cleaning every problem. You are building the habit of noticing what R sees before deciding what to fix.

5.5 The Main Idea

R does not know what you meant. R only knows what object you gave it.

That means you need to ask inspection questions:

  • Does this object exist?
  • What class is it?
  • Is it a vector or a data frame?
  • What are the column names?
  • Are numbers stored as numbers or text?
  • What does the error message actually say?

These questions should come before editing code.

5.6 Build The Workflow

5.6.1 Create Simple Objects

An object stores a value under a name.

my_number <- 5
my_text <- "5"

These look similar to a human, but they are different to R.

class(my_number)
class(my_text)

my_number is numeric. my_text is character. R can do arithmetic with my_number. R treats my_text as text.

5.6.2 Inspect A Vector

A vector holds several values of the same basic type.

my_values <- c(10, 20, 40)
class(my_values)
length(my_values)
mean(my_values)

Before you calculate a mean, check that the object is numeric. If one value is accidentally stored as text, the calculation can fail or behave differently than you expect.

5.6.3 Inspect A Data Frame

A data frame is a table.

soil_samples

Useful inspection functions are:

class(soil_samples)
str(soil_samples)
names(soil_samples)
summary(soil_samples)

Read those as:

  • class() asks what kind of object this is.
  • str() asks for the structure.
  • names() asks for column names.
  • summary() asks for a quick summary of each column.

You should run inspection code often. It is not wasted time.

5.6.4 Use The Environment Pane

The Environment pane (called Variables in Positron) is a diagnostic tool. It shows objects currently stored in your R session.

Use it to ask:

  • Is the object there?
  • Is the spelling exactly what I expected?
  • Is it a data frame, vector, or something else?
  • How many rows and columns does it have?

If the object is not in the Environment pane, R cannot use it.

5.6.5 Read An Error Message

When R gives an error, pause before changing the code.

Ask three questions:

  1. What line produced the error?
  2. What object or function is named in the error?
  3. What is R saying it could not do?

For example:

sites_df$name

If sites_df does not exist, R may say that the object was not found. Start with the smallest possible question: does the object exist, and is the name spelled exactly the same way?

5.7 Read The Code

This line is common in data analysis:

soil_samples$ph

Read it as:

  • use the object named soil_samples
  • look inside that data frame
  • pull out the column named ph

The $ operator is useful, but it is strict. If the object name is wrong, the code fails. If the column name is wrong, the code fails. If ph exists but is stored as character, numeric summaries may fail or give warnings.

An error is not a personal failure. It is information about the object, the code, or the assumption you just tested.

5.8 Check Your Work

Before you say a script is fixed, check:

  • Does the script run from top to bottom?
  • Can you point to each object in the Environment pane?
  • Can you explain the class of each important object?
  • Can you identify the rows and columns in the data frame?
  • Did you read the full error message?
  • Did your fix address the cause of the problem?

5.9 Common Problems

Symptom Likely cause First thing to try
object not found The object was not created, or the name is misspelled Check the Environment pane and spelling
could not find function A package is not loaded, or the function name is misspelled Check the function name and load the package
A mean or summary fails A number may be stored as character Run class() and str()
A column returns NULL The column name does not match Run names()
R shows + and waits The command is incomplete Press Esc, then check parentheses, commas, and quotes
A date looks like text The column has not been parsed as a date Inspect first; parsing comes in a later week

5.10 Try It

Base task:

Open the Module 02 Debugging Clinic repository from the slides and work in debug_this.R. Fix the three intentional bugs. Above each fix, write one short comment that names the cause of the error.

Example:

my_values is a numeric vector that contains three numbers.

Extension:

Before fixing one of the bugs, write down:

  • the exact error message
  • the object or function involved
  • what you checked first
  • what change fixed the cause

5.11 Connect To The Assignment

This week is a supported debugging clinic. Save your completed debug_this.R file, and replace the README paragraph with 2-3 sentences of your own. At the start of class, we will upload it together: accept Assignment 02 - Debugging Clinic in Classroom 50, clone it, copy your fixed script over the starter, commit, and push. Assignment 02 is due Tuesday, Sep 22, 11:59 PM.

This is the last time the upload is walked through in class. Assignment 03 is already open in Classroom 50; you do it at home, push from home, and it is due Sunday, Sep 27, 11:59 PM.

Two conventions that hold for every assignment from here on:

  • Written answers (explanations, interpretations, the AI note) go as # comments inside the script, under the matching section heading. Not in a separate document, not in the README.
  • Feedback on your push arrives as a GitHub Issue in your repository (the Issues tab). Your Module 01 feedback is there now; read it before you push Assignment 02.

Future weeks will add more functions, more files, and messier data. The debugging habit stays the same:

  1. inspect the object
  2. read the error
  3. make one small change
  4. run the code again
  5. check the result

5.12 Using Feedback While Debugging

Debugging is where outside help is tempting. It is also where you learn the most if you slow down before accepting a fix.

Before asking a classmate, instructor, GitHub check, or AI tool for help, write three short notes:

  1. What I expected: What object, value, column, plot, or message did you expect?
  2. What happened instead: Copy the exact error message or describe the unexpected output.
  3. What I already checked: Name the object, class, column names, package, or file path you inspected.

Useful help prompt:

I am a beginning R user learning objects and debugging.
I expected:
[plain-language expectation]
R did this instead:
[paste exact error message or output]
I already checked:
[list class(), str(), names(), file path, or package checks]
Please explain what the error means and suggest the next two checks.
Keep the response focused on the next checks rather than a rewritten script.

If the feedback gives you code, do not paste it immediately. First, explain in one sentence why that change should fix the problem. If you cannot explain it yet, ask for a simpler explanation.

5.13 Finish The Professional Workflow

Before you stop:

  • Run the script from top to bottom.
  • Confirm that intentional bugs are fixed or clearly marked.
  • Save debug_this.R after each successful fix.
  • Keep the error-explanation comments above the three fixes.
  • Replace the README paragraph with your own description.
  • Bring the saved file to class for the supported upload, then push from home for Assignment 03.

Professional analysts still make errors. The professional habit is to make errors visible, diagnose them carefully, and leave a record of the fix.