R编程中搜索并返回指定名称的对象——get()函数
R 语言中的get()
函数用于返回具有指定名称作为函数参数的对象。这是另一种打印对象值的方法,就像 print函数一样。此函数还可用于将一个对象复制到另一个对象。
Syntax: get(object)
Parameters:
object: Vector, array, list, etc.
示例 1:
# R program to illustrate
# the use of get() Function
# Creating vectors
x1 <- c("abc", "cde", "def")
x2 <- c(1, 2, 3)
x3 <- c("M", "F")
# Calling get() Function
# for print operation
get("x2")
# Assigning to another vector
x4 <- get("x3")
print(x4)
输出:
[1] 1 2 3
[1] "M" "F"
示例 2:
# R program to illustrate
# the use of get() Function
# Creating vectors
x1 <- c("abc", "cde", "def")
x2 <- c(1, 2, 3)
# Creating a list of vectors
list1 <- list(x1, x2)
# Calling get() Function
# for print operation
get("list1")
输出:
[[1]]
[1] "abc" "cde" "def"
[[2]]
[1] 1 2 3