将R中data.frame的名称提取为字符

时间:2017-07-18 20:25:59

标签: r dataframe

如何在R中提取<body> <div id='map-container'> <div class="text">TEXT</div> <div id='map'></div> <div class="text">TEXT</div> <div id='map2'></div> <div class="text">TEXT</div> </div> <script src="script.js"></script> </body> 的名称作为字符?例如,如果我data.frame名为data.frame,我想得到&#34; df&#34;作为角色对象。

2 个答案:

答案 0 :(得分:14)

a <- data.frame()
deparse(substitute(a))
[1] "a"

这也是plot知道放在轴上的内容的方式

答案 1 :(得分:3)

如果您想要获取多个数据帧,则可以使用ls.str(mode = "list")。我们使用list是因为数据帧存储为列表。

例如:

# make two dataframes
mydf1 <- data.frame(a = letters[1:10], b = runif(10))
mydf2 <- data.frame(a = letters[1:10], b = runif(10))

# see that dataframes are stored as lists:
storage.mode(mydf1)
[1] "list"

# store the names of the dataframes
names_of_dataframes <- ls.str(mode = "list")

# see the name of the first dataframe
names_of_dataframes[1]
[1] "mydf1"

# see the name of the second dataframe
names_of_dataframes[2]
[1] "mydf2"

请注意,此方法还将包含全局环境中其他列表对象的名称。 ls.str允许您选择要搜索的环境,以便将数据帧存储在与其他列表对象不同的环境中。