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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.