天天看点

RNote103---R中的变量名操作

title: “R中的变量名操作”

author: “刘栋”

date: “2018年7月12日”

output: word_document

knitr::opts_chunk$set(echo = TRUE)      

  目的明确:循环赋值时,希望取出的字符串直接作为变量名。

exists

  查看当前工作空间是否存在该对象。

# 1.注意输入的是字符串
# 2,返回 FALSE
exists("test")      
# 返回TRUE
test <- 1:10
test_name <- "test"
exists(test_name)      

get0&get

  直接获取变量名为​

​x​

​​的值(x=string,要求是字符串),如果不存在​

​get0​

​​返回​

​NULL​

​​,​

​get​

​​返回​

​Error​

​。

# 返回 1  2  3  4  5  6  7  8  9 10
get0("test")
# 返回 1  2  3  4  5  6  7  8  9 10
get("test")      
# 返回 NULL
get0("tes")
# 返回 Error in get("tes") : object 'tes' not found
get("tes")      

assign

# 返回 FALSE,即当前工作空间内没有tes变量
exists("tes")
# 返回 生成tes变量
assign("tes",c("a","b","c"))
# 获取变量名为"tes"的变量
get0("tes")