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 

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 <a href=help with object not found error in r" width="300" height="163" />

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.

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.

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.

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

Examples For Common Use

Resources to help you simplify data collection and analysis using R. Automate all the things!