Skip to content
Related Articles

Related Articles

Improve Article

Check if an Object of the Specified Name is Defined or not in R Programming – exists() Function

  • Last Updated : 16 Jun, 2020

exists() function in R Language is used to check if an object with the names specified in the argument of the function is defined or not. It returns TRUE if the Object is found.

Syntax: exists(name)

Parameters:
name: Name of the Object to be searched

Example 1:




# R program to check if
# an Object is defined or not
  
# Calling exists() function
exists("cos")
exists("diff")
exists("Hello")

Output:



[1] TRUE
[1] TRUE
[1] FALSE

Example 2:




# R program to check if
# an Object is defined or not
  
# Creating a vector
Hello <- c(1, 2, 3, 4, 5)
  
# Calling exists() function
exists("Hello")

Output:

[1] TRUE

In the above examples, it can be seen that when there was no object named “Hello” defined in the first code, the exists() function returned FALSE, whereas after the declaration of the “Hello” Object, the exists() function returned TRUE. This means that the exists() function checks for pre-defined and user-defined, both objects.

My Personal Notes arrow_drop_up
Recommended Articles
Page :