Object not found
Matches: object '([^']+)' not found
WHAT THIS ERROR MEANS
R cannot find a variable or object with the name you used. This usually means you haven't created it yet, misspelled it, or it exists in a different environment.
WHY IT HAPPENS — 4 COMMON CAUSES
1. The line that creates it never ran
The most common case, and the least obvious one in an interactive session. If you restarted R, opened a fresh session, or ran a script from the middle, the assignment simply has not executed. R keeps no memory of objects between sessions unless you deliberately saved the workspace, so code that worked yesterday can fail today on a name you are certain you defined.
2. A typo, or the wrong case
R is case-sensitive: my_data, my_Data and myData are three different names. This bites hardest with names that came from somewhere else — a column imported from a CSV, or a variable a colleague wrote. The error text quotes the exact name R looked for, so compare it character by character against what you actually created.
3. It was created inside a function
Assignments inside a function body live in that function’s local environment and are discarded when the function returns. Calling the function does not leave its intermediate variables behind. To keep a value you must return it and assign the result at the call site.
4. You referenced a column as if it were a variable
Inside dplyr verbs and ggplot2’s aes(), bare column names resolve against the data frame. Outside them they do not. Writing mean(revenue) at the top level asks R for a global object called revenue, which does not exist — you need df$revenue.
HOW TO DIAGNOSE WHICH ONE IT IS
- Run ls() to list every object currently in your global environment. If the name is absent, it was never created in this session.
- Run exists("my_data") for an unambiguous TRUE/FALSE that also searches attached packages.
- Suspect a typo? ls(pattern = "dat") does a partial match and will surface near-misses like my_dat or mydata.
- If the name should be a column, run names(df) and check it against the name in the error message.
- Still puzzled? environmentName(environment()) confirms which environment you are actually evaluating in — useful inside functions and Shiny reactives.
HOW TO FIX IT
1. Check for typos in the variable name.
2. Make sure you ran the line that creates the variable before using it.
3. Use ls() to see what objects exist in your environment.
CODE EXAMPLES
# Forgot to create the variable mean(my_data)
my_data <- c(1, 2, 3, 4, 5) mean(my_data)
MORE SCENARIOS THAT TRIGGER THIS
A column referenced without its data frame
# revenue is a column, not a global object mean(revenue)
mean(df$revenue) # or, inside a dplyr verb where bare names resolve: df |> summarise(avg = mean(revenue))
This is the single most frequent version of the error for people coming from Stata or SAS, where variables are addressed bare. In base R you must say which data frame the column belongs to.
A value created inside a function
summarise_sales <- function(df) {
total <- sum(df$amount)
}
summarise_sales(sales)
total # Error: object 'total' not foundsummarise_sales <- function(df) {
total <- sum(df$amount)
total # return it
}
total <- summarise_sales(sales)The function ran fine — total existed, briefly, inside it. Without an explicit return the value is discarded. Note that R returns the last expression automatically, so naming it on the final line is enough.
A pipeline stage that dropped the column
df |> select(region, units) |> mutate(margin = revenue / units) # Error: object 'revenue' not found
df |> select(region, units, revenue) |> mutate(margin = revenue / units)
select() had already discarded revenue by the time mutate() ran. When a pipeline throws this, the column usually existed at the start — check what each stage keeps by running the pipe one step at a time.
PACKAGE-SPECIFIC NOTES
Inside dplyr, ggplot2 and data.table, bare names are resolved against the data first through tidy evaluation, so this error usually means the column genuinely is not there at that point in the pipeline rather than that a global variable is missing. If you need to be explicit about which you mean, .data$revenue forces a column lookup and .env$revenue forces a variable lookup — worth using when a column and a local variable share a name, which otherwise produces silently wrong results rather than an error.
FUNCTIONS WORTH KNOWING
ls()exists()get()rm()environment()str()names()FREQUENTLY ASKED QUESTIONS
Why does the object exist when I run the line manually but not when I source the script?
source() evaluates in its own environment by default. Assignments made inside the sourced file do not land in your global environment unless you call source("file.R", local = FALSE) or the script assigns with <<-. Running lines manually assigns straight into the global environment, which is why it appears to work.
What is the difference between this error and "object of type closure is not subsettable"?
This error means the name is not bound to anything at all. The closure error means the name is bound — to a function — and you tried to index it. If you write df <- read.csv(...) but the read fails, df stays undefined and you get object not found; if you never assign df at all, R finds the stats function df() instead and you get the closure error.
Does rm(list = ls()) cause this?
Yes, and deliberately so. It clears every object in the global environment, which is exactly what you want at the top of a reproducible script but will break any later code that assumed something was already loaded.
Why do I get it inside a Shiny app but not in the console?
Shiny reactives evaluate in their own environments. A value assigned in server() is not visible inside a renderPlot() block unless it was created with reactive() or reactiveVal() and is read with the trailing parentheses, as in my_data().
Still stuck?
Paste your code in RChat and the AI will fix this error in context.