R语言 搜索并返回具有指定名称的对象 – get() 函数
R语言中的 get() 函数用于返回一个具有函数参数指定名称的对象。这是另一种打印对象值的方法,就像打印函数一样。这个函数也可以用来将一个对象复制到另一个对象。
语法: get(object)
参数:
对象: 向量、数组、列表等。
例子 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