Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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:




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


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:




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


Output:

$x
[1] 1 2 3

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

$x1
[1] "Not Found"
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Jun, 2020
Like Article
Save Article
Previous
Next
Similar Reads