在R编程中返回一个具有指定名称的对象——get0()和mget()函数
在 R 编程中, get0()
和mget()
函数的工作方式类似于get()
函数。它用于搜索并返回具有作为参数传递给它的指定名称的对象。
get0()函数
get0()
函数与get()
()函数具有相同的语法,但添加了一个新参数,如果未找到数据对象,该参数将返回输出。显然,这在某种程度上造成了用户生成的异常。
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
例子:
# 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")
输出:
[1] 1 2 3
[1] "not found"
mget()函数
R 编程中的mget()
函数类似于get()
函数,但它能够搜索多个数据对象,而不是get()
函数中的单个对象。
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
例子:
# 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")
输出:
$x
[1] 1 2 3
$y
[1] "a" "b" "c"
$x1
[1] "Not Found"