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:
# 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"
Please Login to comment...