Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Error: object '<myvariable>' not found" mean?

Tags:

r

r-faq

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?

like image 490
Richie Cotton Avatar asked Jan 11 '15 12:01

Richie Cotton


People also ask

What does object not found 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.

Why does R keep saying object not found?

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.

When you run a certain code the error message tells you that object not found what could be the reasons list at least three reasons?

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.

What is an object in Rstudio?

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.


2 Answers

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:

  • The Scope section of the CRAN manual Intro to R and demo(scoping)
  • The Non-standard evaluation chapter of Advanced R
like image 198
3 revs Avatar answered Oct 04 '22 15:10

3 revs


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".

like image 38
Shalini Avatar answered Oct 04 '22 15:10

Shalini