Read, Fix, and Test R Errors
Learn to read error messages, fix common bugs, verify your code, and know when to ask for help
Welcome back. Everything on this page runs real R in your browser — same setup as the other coding activities. Errors are normal. When R stops with red text, it is telling you something specific went wrong. Your job is to read that message, fix the line, and check that the fix worked.
The first time you click Run Code, your browser downloads R once (a few seconds). After that it is quick.
R prints errors from the inside out:
- Start at the bottom line — that is usually what went wrong (
object 'x' not found,could not find function, and so on). - Read upward for the line number and function name where R got stuck.
- Fix one thing, run again, and see whether the message changes.
Errors are information, not failure. Every experienced programmer sees them all day.
1. Object not found
See the error
Someone tried to compute the mean body mass of penguins. Run their code below.
Read the bottom line of the error: object 'body_mass' not found. R is telling you that name was never created in memory — but what should body_mass have been?
Inspect the data
Before you fix anything, load the penguins dataset and see which columns are available. Run library(palmerpenguins) and glimpse(penguins) below. Look for a body-mass column measured in grams.
Fix the code
Now write code that creates body_mass from the right column and computes its mean in grams.
R looks up object names in memory. If you see object '...' not found, create the object with <- before you use it.
From your glimpse(penguins) output, find the body-mass column measured in grams — its name ends in _g.
Example with mtcars:
mpg_vals <- mtcars$mpg
mean(mpg_vals, na.rm = TRUE)Apply the same two-step pattern: assign that column from penguins, then call mean() on it.
body_mass <- penguins$body_mass_g
mean(body_mass, na.rm = TRUE)R looks up names in memory. If body_mass was never assigned, R cannot find it.
2. Wrong column name
This line fails because the column name is slightly wrong. Fix it and run head() on the result to confirm you have numbers.
The $ operator pulls one column from a data frame. Column names must match exactly — run ?Extract or inspect names first:
glimpse(mtcars)
head(mtcars$mpg)If $ returns NULL, the name is wrong. Use glimpse() on penguins to find the correct body-mass column name.
body_mass <- penguins$body_mass_g
head(body_mass)$ returns NULL when the column name does not match exactly — R is case-sensitive and picky about spelling.
3. Function typo
filtr is not a real function. Fix the typo, keep only Adelie penguins, and run the line.
could not find function "..." usually means a typo in the function name. From ?filter: filter(.data, ..., .preserve = FALSE).
Correct spelling example:
filter(mtcars, cyl == 4)Compare your function name letter by letter with the dplyr verb for keeping rows.
adelie <- filter(penguins, species == "Adelie")
adeliecould not find function "filtr" means R has no function with that exact name — often a one-letter typo.
4. Type mismatch
Run this broken line on purpose. Read the error, then fix it so R averages a numeric column, not text in quotes.
From ?mean: the first argument must be numeric. Text in quotes is character, not a number.
Broken vs. fixed pattern:
mean("20") # character — wrong type
mean(mtcars$mpg, na.rm = TRUE) # numeric column — correct typePass a numeric column from penguins, not a quoted string.
mean(penguins$body_mass_g, na.rm = TRUE)non-numeric argument to binary operator (or similar) means R expected a number and got something else — often quotes around a value that should be numeric.
5. Missing na.rm = TRUE
This runs without crashing but returns NA. Fix it so R skips missing values and returns the actual average.
From ?mean: mean(x, na.rm = FALSE) — when na.rm = FALSE (the default), any missing value makes the whole result NA.
Example:
mean(mtcars$mpg, na.rm = TRUE)Add na.rm = TRUE to your mean() call on the penguin body-mass column.
mean(penguins$body_mass_g, na.rm = TRUE)Two penguins have missing body mass. Without na.rm = TRUE, mean() returns NA rather than guessing.
6. Test that your fix worked
Fixing a line is not the end. Verify before you move on:
- Re-run the line that failed
- Use
glimpse()orstr()to confirm structure - After a
filter(), checknrow()— did you keep the rows you expected? - Use
print()orhead()when output looks suspicious
Below is a working filter that drops penguins with missing body mass. Add a line that prints how many rows remain. You should see 342.
From ?nrow: nrow(x) returns the number of rows in a data frame — a quick check after filter().
Example:
mtcars |>
filter(cyl == 4) |>
nrow()Count rows in the filtered penguin object named in the exercise.
nrow(penguins_clean)342 rows — two penguins with missing body mass were removed. Row counts are a fast sanity check after every filter or join.
7. Where to get help (without AI)
When you are stuck after reading the error and trying a fix:
- Read the error again — bottom line first, then line number
- Help pages:
?mean,?filter(dense, but official) - Web search: paste the exact error text in quotes
- Compare your line to a working example from lecture, an activity, or a classmate
- Ask a human: office hours, discussion board, instructor, or TA
Try this with a partner. One person reads the error message aloud slowly; the other suggests what to try first. Swap roles. Saying the error out loud often makes the fix obvious.
8. Using generative AI to debug
Generative AI can explain errors in plain language — if you give it enough context. Copy a prompt like this into ChatGPT, Claude, or another tool (when your syllabus allows it):
I am a beginner learning R in an introductory biology course. Here is the code I ran, the full error message, and what I was trying to do: [paste code], [paste error], [one sentence goal]. Please explain the error in plain language, suggest one fix, and show a corrected example using the Palmer Penguins dataset. Do not write my graded assignment for me.
Rules that keep AI useful and honest:
- Paste your code and the full error — not just “it doesn’t work”
- Run the suggested fix yourself before submitting anything graded
- Disclose AI use on assignments when the syllabus requires it
9. Course tools you can use
BILD 5 offers several AI-related resources. Names only — your instructor or Canvas will show you where to find each one:
| Tool | What it is for |
|---|---|
| BILD5 bug checker chatbot | Course-specific help with R error messages and debugging steps |
| Schema Study | Socratic study coach for biology concepts — good for understanding what you are analyzing, not for pasting quiz or exam questions |
| Triton GPT | UC San Diego’s campus AI assistant — general help; still verify any code yourself and follow the course AI policy |
None of these replace running and testing your own code. They supplement the read → fix → verify workflow you practiced above.
10. Recap
- Read the error (bottom line, then line number)
- Fix one thing at a time
- Test — re-run, check
nrow(),glimpse(), orhead() - Ask for help — human, help page, search, or AI with full context
If you can do this on your own with Palmer penguins, you can do it on Coding Assignments and in DataHub.
Keep playing
Intentionally break a line, read the error, and fix it without looking at the hints. For example, typo a column name or forget na.rm = TRUE.
The column is flipper_length_mm — not flipper_lenght_mm. Practice the full loop: run, read, fix, verify.