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

Gau*_*sal 7 r dataframe

如何将data.frameR中的名称作为字符提取?例如,如果我已data.frame命名df,我想将"df"作为角色对象.

Van*_*man 19

a <- data.frame()
deparse(substitute(a))
[1] "a"
Run Code Online (Sandbox Code Playgroud)

这也是如何plot知道放在轴上的内容


mee*_*ram 6

如果您有多个要检索其名称的数据框,则可以使用ls.str(mode = "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"
Run Code Online (Sandbox Code Playgroud)

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