# Check if Object is Defined (exists in R) | 4 Examples: Vector, Variable, Function, Error

In this R tutorial, I’ll show you how to check whether a **data object exists** in your R programming environment. The tutorial is mainly relying on the usage of the exists R function. Let’s start with the basic R syntax and the definition of the exists function:

**Basic R Syntax:**

exists(x) |

exists(x)

**Definition:**

The exists function checks whether an R object is defined in the R environment.

The exists function is very flexible and can be applied to different R objects such as vectors, variables of a data.frame, or functions. In the following article, I’m going to show you **four examples** for the usage of exists.

Let’s dive into it!

## Example 1: Apply exists() Function to Vector

Probably the easiest and most intuitive way to apply the exists function is when we use it for vectors. Let’s create a simple vector for our first example:

x <- c(2, 9, 5, 3) # Create example vector |

x <- c(2, 9, 5, 3) # Create example vector

And not let’s use exists() to check if this vector is properly defined in our R environment:

exists("x") # Apply exists function to vector # TRUE |

exists("x") # Apply exists function to vector # TRUE

The exists function returns *TRUE* to the RStudio console. In other words: Yes, the vector x exists.

For comparison, let’s also apply the exists function to a vector that we did not define before:

exists("y") # Apply exists function to non-existent vector # FALSE |

exists("y") # Apply exists function to non-existent vector # FALSE

The exists command returns *FALSE*, i.e. the vector *y* does not exist.

That’s it! With this simple code we can check the existence of any data element we want.

However, when we want to check for the existence of variables in a data.frame, it get’s slightly more difficult. But that’s what I’m going to show you in the next example.

## Example 2: Check if Variable in Data Frame is Defined

Let’s first create a data frame for this example:

data <- data.frame(x1 = c(3, 9, 9, 2, 2), # Create example data.frame x2 = c(7, 8, 1, 4, 1)) |

data <- data.frame(x1 = c(3, 9, 9, 2, 2), # Create example data.frame x2 = c(7, 8, 1, 4, 1))

If we want to check for the existence of certain column names in this data frame, we need to attach the data first…

attach(data) # Attach data.frame |

attach(data) # Attach data.frame

…and then we can check for the existence of variable names in our data:

exists("x1") # Apply exists to variable of data # TRUE |

exists("x1") # Apply exists to variable of data # TRUE

*x1* exists…

exists("x3") # Apply exists to non-existent variable # FALSE |

exists("x3") # Apply exists to non-existent variable # FALSE

…but *x3* doesn’t.

Hint: Don’t forget to detach the data afterwards. If you keep the data attached it might lead to confusion in the following programming steps.

detach(data) # Detach data.frame |

detach(data) # Detach data.frame

## Example 3: Check if Function Exists

The exists R command can also be used to check for the existence of functions. For instance, we can check whether the function rbind.fill of the plyr package is defined:

exists("rbind.fill") # Apply exists to check for function # FALSE |

exists("rbind.fill") # Apply exists to check for function # FALSE

As you can see, the function is not defined.

Why? Because we didn’t install and load the plyr package:

install.packages("plyr") # Install plyr package library("plyr") # Load plyr package to R |

install.packages("plyr") # Install plyr package library("plyr") # Load plyr package to R

Now let’s apply exactly the same code as before:

exists("rbind.fill") # Apply exists to check for function # TRUE |

exists("rbind.fill") # Apply exists to check for function # TRUE

The exists function returns TRUE – rbind.fill is available now.

## Example 4: Error in exists(x) : Object ‘X’ not Found

In the fourth example, I want to show you an error message that might appear when exists is used:

exists(x) # exists function returns error |

exists(x) # exists function returns error

**Figure 1: Error in exists(x) : object ‘x’ not found.**

Did you find the mistake? We didn’t put the data object we are looking for in quotes (i.e. “x”). Let’s try this again with quotes:

exists("x") # Apply exists function correctly # TRUE |

exists("x") # Apply exists function correctly # TRUE

Looks good!

## Video: The R Environment

In this tutorial, we spoke a lot about the environment of the R programming language. However, in if you are an unexperienced R user, the concept of the R environment may be difficult to understand.

But no worries! In the following video of the Bioinformatics DotCa YouTube channel, the speaker explains the functionality of the R environment in detail. Enjoy the video and see you next time!

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R