If you try to refer to an object that has not been defined in an R code block or before it, you will raise the error object not found. The R interpreter could not find the variable mentioned in the error message. You can check if a variable exists using ls or exists, then create the variable if it does not exists.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
Example #1: Missing Variable definition
The easiest way to reproduce the error is to reference a variable name that we did not previously define, for example:
> x
Error: object 'x' not found
If we try to call a function and pass an undefined variable as an argument, we will also raise the error:
> mean(x)
Error in mean(x) : object 'x' not found
Solution
We can solve this error by defining the variable. Let’s look at the revised code.
> x <- c (44, 31, 20, 5, 77) > exists("x")
In the above code, we use the c()
function to create a vector of numbers. We can verify that the variable exists by using the built-in exists()
method. If exists()
returns TRUE, then the variable exists. Otherwise, if it returns FALSE, the variable does not exist.
[1] TRUE
We can also list all of the defined variables using ls().
> ls()
[1] "x"
We can use an if statement only to call a function if a variable exists:
> if (exists("x")) mean(x)
Let’s run the code to see the result:
[1] 35.4
We successfully calculated the mean of the numbers in the vector with the variable name “x
“.
Example #2: Subset DataFrame with Missing Column Name
The error can also occur when trying to subset a data frame, and the column name we reference does not exist. Let’s look at an example of a data frame with random numbers sampled from the normal distribution.
> d <- data.frame(a = rnorm(10)) > d
a 1 -0.71675335 2 -0.51653842 3 -0.54683104 4 0.36271733 5 -0.13695605 6 0.08996665 7 1.06901831 8 -0.32218248 9 -0.17214685 10 2.50305373
Let’s try to subset the column with the name “b
” under the condition that the values are greater than zero:
> subset(d, b>0)
Let’s run the code to see the result:
Error in eval(e, x, parent.frame()) : object 'b' not found
The error occurs because column “b” does not exist in the data frame.
Solution: Check if a Column Exists in data frame using %in% and grepl
We can verify if a column exists using %in%
and grepl
.
Using %in%
We can use the %in%
operator to identify if a column belongs to a data frame:
> "b" %in% names(d)
[1] FALSE
We can see that column “b
” does not exist in the data frame, whereas column “a
” does exist:
> "a" %in% names(d)
[1] TRUE
Using grepl
We can use grepl
to check for a match in the column names. If grepl
returns FALSE, then the column name is not present in the data frame.
grepl("^b$", names(d))
[1] FALSE
We can see that column “b
” does not exist in the data frame, whereas column “a
” does exist:
grepl("^a$", names(d))
[1] TRUE
We can use an if statement to check for membership in the data frame before performing subsetting. Let’s look at the revised code:
> if("a" %in% names(d)) subset(d, a > 0)
a 4 0.36271733 6 0.08996665 7 1.06901831 10 2.50305373
We successfully retrieved the positive values in column “a
“.
Summary
Congratulations on reading to the end of this tutorial! The error object not found occurs when you try to reference an undefined variable in your R code. You can check if an object exists using exists()
or ls()
, and you can check if a column exists in a data frame using %in%
or grepl
.
For further reading on R related errors, go to the articles:
- How to Solve R Error: Names do not match previous names
- How to Solve R Error: $ operator is invalid for atomic vectors
Go to the online courses page on R to learn more about coding in R for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.