Return an Object with the specified name in R Programming – get0() and mget() Function

In R programming, get0() and mget() function works similar to get() function. It is used to search and return the object with the specified name passed to it as argument.

get0() Function

get0() function has the same syntax as get() function but there is an addition of a new parameter which returns an output if the data object is not found. Clearly, this is making user-generated exception in a way.

Syntax: get0(x, mode, ifnotfound)

Parameters:
x: represents data object to be searched
mode: represents type of data object
ifnotfound: represents the output that has to be returned when x is not found

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Define data objects
x <- c(1, 2, 3)
y <- c("a", "b", "c")
  
# Searching using get0() function
get0("x", ifnotfound = "not found")
get0("x1", ifnotfound = "not found")

chevron_right


Output:

[1] 1 2 3
[1] "not found"

mget() function

mget() function in R programming works similar to get() function but, it is able to search for multiple data objects rather than a single object in get() function.

Syntax: mget(x, mode, ifnotfound)

Parameters:
x: represents character vector of object names
mode: represents type of data object
ifnotfound: represents the output that has to be returned when x is not found

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Defining data objects
x <- c(1, 2, 3)
y <- c("a", "b", "c")
  
# Searching using mget() function
mget(c("x", "y", "x1"), ifnotfound = "Not Found")

chevron_right


Output:

$x
[1] 1 2 3

$y
[1] "a" "b" "c"

$x1
[1] "Not Found"
My Personal Notes arrow_drop_up

Recommended Posts:


Blockchain Enthusiast

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.