简体   繁体   English

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

[英]Extract name of data.frame in R as character

How can I extract the name of a data.frame in R as a character?如何将 R 中的data.frame的名称提取为字符?

For example, if I have data.frame named df , I want to get "df" as a character object.例如,如果我有名为df data.frame ,我想将“df”作为字符对象。

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

This is also how plot knows what to put on the axes这也是plot如何知道在轴上放什么

If you've got more than one dataframe that you want to retrieve the name of, you can use ls.str(mode = "list") .如果您有多个要检索其名称的数据ls.str(mode = "list") ,则可以使用ls.str(mode = "list") We use list because dataframes are stored as lists.我们使用列表是因为数据帧存储为列表。

For example:例如:

# 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"

Note that this method will also include the names of other list objects in your global environment.请注意,此方法还将包括全局环境中其他列表对象的名称。 ls.str allows you to select the environment you want to search so you could store your dataframes in a different environment to other list objects. ls.str允许您选择要搜索的环境,以便您可以将数据帧存储在与其他列表对象不同的环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM