How to fix the R Error: object not found (object name reference errors)

We’ve all been there – frantically searching for something that we just can’t seem to find. It’s like trying to find a needle in a haystack, or your keys in the morning before coffee. In the world of R programming, this frustration takes the form of the dreaded ‘object not found’ error message. It’s passive aggressive computer talk for “we know what you want, we just acknowledge that we have it”.

The “object not found” error can be one of the hardest to diagnose, and yet it can be quite easy to fix. This is especially true if you’re dealing with small variances in naming conventions or data types. You’ll sit there staring at the screen swearing that you’re going mad, since the names look similar. However, a slight difference or omission managed to creep into your code…..

Why You’re Seeing This Error Message

R generates an “object not found” error when the object name that you’re requesting (in your code) isn’t registered with the program. This is one of the tricky aspects of trouble-shooting this message: while you think you know what you’re asking for, the computer obviously doesn’t. You are literally looking for something that does not exist in your data set or data frame.

# Error Example - R object not found
> y
 Error: object 'y' not found

Here’s an example of this error in the wild…. see the code below…

# Attempt to access an object that doesn't exist
my_data <- iris
head(my_data)
tail(my_data2)

Since my_data2 hasn’t been defined in the program (and allocated within memory), R promptly comes to a complete halt and complains about the situation.

Other Reasons You May Get This Error

r error object not found  object not found error in r  how to fix r object not found  help with object not found error in r

What triggers the error is the program not being able to find an object name being called. While this is what technically causes this error the real cause boils down to three simple programming mistakes.

  • The object is defined in a different environment
  • The object has been deleted by other R code in your program
  • The object wasn’t created due a warning or error elsewhere in your program


That being said, Typographical errors are probably going to be the most common cause of getting this error. This is because when retyping an object’s name there is always the possibility of hitting the wrong key.

How to Fix This Error

Fixing this R error comes down to dotting your i’s and crossing your t’s. You’ve got either a typo or a sequencing error. Walk through your code to look for it.

  • Check for spelling issues and types. This is where doing a copy-paste between the different parts of your code can sometime help.
  • Check to make sure that you have actually defined the object.
  • See if there is a reason why the routine was called before you defined the object.
  • Make sure you haven’t accidentally deleted the object or suppressed an error in the upstream code

In the case where the routine is being called before you define the object, the exists() function will prevent you from getting this error.

# Preventing Error: R object not found
> x = c(1, 2, 3)
 > exists("x")
 [1] TRUE
 > exists("y") 
 [1] FALSE
 > if(exists("y")) y else x
 [1] 1 2 3
 > y = c(4, 5, 6, 7)
 > if(exists("y")) y else x
 [1] 4 5 6 7

The key to using the exists() function to avoid the object not found error is to use it as the condition in an “if statement” that prevents the calling of the object if it does not yet exist. In most cases, all you will need to do is find and fix a typographical error. However, the exists() function gives you a way to check for it in your code.

Addresses Error: R error object not found

We hope our simple tutorial on fixing this error was helpful, and encourage you to check out some of our other helpful tips:

Scroll to top
Privacy Policy