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返回NULLget返回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

  assign(x, value),x为字符串,value为具体变量值,执行代码,即可生成x变量。

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

                       2018-07-12 于南京市建邺区新城科技园

你可能感兴趣的:(★★★R软件,#,★★R软件基础)