I got the error message:
Error: object 'x' not found
Or a more complex version like
Error in mean(x) : error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
What does this mean?
One common error you may encounter in R is: Error: object 'x' not found. This error usually occurs for one of two reasons: Reason 1: You are attempting to reference an object you have not created. Reason 2: You are running a chunk of code where the object has not been defined in that chunk.
6.2 Error: object not foundThis error usually occurs when your R Markdown document refers to an object that has not been defined in an R chunk at or before that chunk. You'll frequently see this when you've forgotten to copy code from your R Console sandbox back into a chunk in R Markdown.
While this is what technically causes this error the real cause boils down to three simple programming mistakes. Forgetting to define the object. Typographical errors when calling an object. Calling the routine before you define the object.
We can do object oriented programming in R. In fact, everything in R is an object. An object is a data structure having some attributes and methods which act on its attributes. Class is a blueprint for the object. We can think of class like a sketch (prototype) of a house.
The error means that R could not find the variable mentioned in the error message.
The easiest way to reproduce the error is to type the name of a variable that doesn't exist. (If you've defined x
already, use a different variable name.)
x ## Error: object 'x' not found
The more complex version of the error has the same cause: calling a function when x
does not exist.
mean(x) ## Error in mean(x) : ## error in evaluating the argument 'x' in selecting a method for function 'mean': Error: object 'x' not found
Once the variable has been defined, the error will not occur.
x <- 1:5 x ## [1] 1 2 3 4 5 mean(x) ## [1] 3
You can check to see if a variable exists using ls
or exists
.
ls() # lists all the variables that have been defined exists("x") # returns TRUE or FALSE, depending upon whether x has been defined.
Errors like this can occur when you are using non-standard evaluation. For example, when using subset
, the error will occur if a column name is not present in the data frame to subset.
d <- data.frame(a = rnorm(5)) subset(d, b > 0) ## Error in eval(expr, envir, enclos) : object 'b' not found
The error can also occur if you use custom evaluation.
get("var", "package:stats") #returns the var function get("var", "package:utils") ## Error in get("var", "package:utils") : object 'var' not found
In the second case, the var
function cannot be found when R looks in the utils
package's environment because utils
is further down the search
list than stats
.
In more advanced use cases, you may wish to read:
demo(scoping)
While executing multiple lines of code in R, you need to first select all the lines of code and then click on "Run". This error usually comes up when we don't select our statements and click on "Run".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With