How to Fix in R: object not found


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.

The following examples how to resolve this error in each of these scenarios.

Example #1: Object not found when object does not exist

Suppose we use the following code to display a data frame that we have not created:

#create data frame
my_df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                    points=c(99, 90, 86, 88, 95),
                    assists=c(33, 28, 31, 39, 34),
                    rebounds=c(30, 28, 24, 24, 28))

#attempt to display data frame
my_data

Error: object 'my_data' not found

We receive an error because the object my_data does not exist.

Instead, we need to type the correct name of the data frame that we created:

#display data frame
my_df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

This time we’re able to display the data frame without an error because we used the correct name.

Note that we can also use ls() to display all object names in our current environment and exists() to check if a specific object name exists:

#display the names of all objects in environment
ls()

[1] "df"    "my_df" "x"

#check if my_data exists
exists('my_data')

[1] FALSE

We can see that exists(‘my_data’) returns FALSE, which explains why we received an error when we attempted to display it.

Example #2: Object not found when incorrect chunk of code is highlighted

Another reason that we might receive an object not found error is because we have highlighted a chunk of code to run in RStudio that doesn’t contain the name of the object we’re attempting to reference.

For example, consider the following screenshot where we highlight rows 3 through 5 and attempt to calculate the mean of a value named x:

object not found error in R

Since we created the vector named x in row 2, we receive an error because we haven’t actually created that vector in the chunk of code that we highlighted.

If we instead make sure that we highlight the whole chunk of code we’re interested in, we won’t receive any error:

Notice that RStudio displays the mean of vector x without any errors this time.

Additional Resources

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: contrasts can be applied only to factors with 2 or more levels

3 Replies to “How to Fix in R: object not found”

  1. hello, please help. i have my object created and i have checked it with the head() function but when i check it with the exists() function it says object not found and its not allowing me select() the object i need for my code to run. How can i fix this please?

  2. I am using R studio and i am getting error below, how can i resolve it? I am calling cvs file but then it is giving this stata file error

    Error: object ‘data.dta’ not found
    Execution halted

    1. Hi Allan…The error you’re encountering suggests that R is looking for a Stata file (`.dta`), but it seems like you’re trying to read a CSV file instead. The error message indicates that R is attempting to access an object or file that doesn’t exist or hasn’t been loaded correctly.

      ### Steps to Resolve the Issue:

      1. **Ensure You’re Reading the Correct File Type**:
      – If you’re trying to read a CSV file, make sure you’re using the correct function for reading CSV files, such as `read.csv()` or `read_csv()` from the `readr` package.

      Example:
      “`r
      data <- read.csv("yourfile.csv") ``` If the file you're actually trying to read is a Stata file, then use `read_dta()` from the `haven` package. Example: ```r library(haven) data <- read_dta("yourfile.dta") ``` 2. **Check File Path**: - Make sure that the file path you provided is correct. If the file is not in the current working directory, either move the file to the working directory or provide the full path. Example: ```r data <- read.csv("path/to/yourfile.csv") ``` If you're unsure of the current working directory, you can check it using: ```r getwd() ``` 3. **Check for Typos**: - Double-check the filename and the extension to ensure there are no typos or incorrect references. 4. **Ensure Correct Object Name**: - If you've loaded the file into an object, make sure you're referencing the correct object name in subsequent code. Example: ```r # If you loaded the file into an object called `data`, reference it correctly print(data) ``` 5. **Check Script Order**: - Ensure that the line of code where you're reading the CSV file comes before any references to the `data.dta` object. The error might be occurring because the code is trying to access an object that hasn't been created yet. ### Example for Reading a CSV File Correctly: ```r # Correctly reading a CSV file data <- read.csv("yourfile.csv") # Now you can use `data` for further analysis print(head(data)) ``` ### Example for Reading a Stata File: ```r # Load the haven package library(haven) # Correctly reading a Stata file data <- read_dta("yourfile.dta") # Now you can use `data` for further analysis print(head(data)) ``` If you were expecting to read a CSV file but are receiving a Stata file-related error, double-check your code to ensure you're not mixing up file types or object names.

Leave a Reply

Your email address will not be published. Required fields are marked *