exists Function in R (Example)

This article shows how to check whether a data object is existing with the exists() function in the R programming language.

Example for the Application of exists()

Let’s assume that we want to check whether a data object with the name my_data_object does exist (i.e. have we created it somewhere in our previous R syntax?). In this case, we can use the exists function as shown below:

exists("my_data_object")     # Apply exists function
# FALSE

The console of RStudio returns the logical value FALSE. This means that the data object my_data_object does not exist. Let’s create it:

my_data_object <- "hello"    # Create data object

If we apply the exists function again, it returns TRUE.

exists("my_data_object")     # Apply exists function again
# TRUE

The exists function is quite useful in larger R codes and within dynamic for-loops or user-defined functions.

Video Examples for the exists Function

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top