list 将多个数据框放入列表中(智能方式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14954399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:57:35  来源:igfitidea点击:

Put multiple data frames into list (smart way)

rlistloopsdataframe

提问by Martin Petri Bagger

Is it possible to put a lot of data frames into a list in some easy way? Meaning instead of having to write each name manually like the following way:

是否可以以某种简单的方式将大量数据框放入一个列表中?意思是不必像下面这样手动编写每个名称:

list_of_df <- list(data_frame1,data_frame2,data_frame3, ....)

I have all the data frames loaded into my work space. I am going to use the list to loop over all the data frames (to perform the same operations on each data frame).

我已将所有数据框加载到我的工作空间中。我将使用列表遍历所有数据帧(对每个数据帧执行相同的操作)。

回答by Arun

You can use ls()with getas follows:

您可以使用ls()具有get如下:

l.df <- lapply(ls(), function(x) if (class(get(x)) == "data.frame") get(x))

This'll load all data.frames from your current environment workspace.

这将从您当前的环境工作区加载所有 data.frames。

Alternatively, as @agstudy suggests, you can use pattern to load just the data.frames you require.

或者,正如@agstudy 建议的那样,您可以使用模式来加载data.frame您需要的s。

l.df <- lapply(ls(pattern="df[0-9]+"), function(x) get(x))

Loads all data.frames in current environment that begins with dffollowed by 1 to any amount of numbers.

加载data.frame当前环境中以df1开头的所有s到任意数量的数字。

回答by Paul Hiemstra

By far the easiest solution would be to put the data.frame's into a list where you create them. However, assuming you have a character list of object names:

到目前为止,最简单的解决方案是将data.frame's 放入创建它们的列表中。但是,假设您有一个对象名称的字符列表:

list_df = lapply(list_object_names, get)

where you could construct you list like this (example for 10 objects):

您可以在其中构建这样的列表(例如 10 个对象):

list_object_names = sprintf("data_frame%s", 1:10)

or get all the objects in your current workspace into a list:

或者将当前工作区中的所有对象放入一个列表中:

list_df = lapply(ls(), get)
names(list_df) = ls()

回答by agstudy

You can use lswith a specific pattern for example. For example:

例如,您可以使用ls特定模式。例如:

some data.frames:

一些数据框架:

data.frame1 <- data.frame()
data.frame2 <- data.frame()
data.frame3 <- data.frame()
data.frame4 <- data.frame()

list(ls(pattern='data.fra*'))
[[1]]
[1] "data.frame1" "data.frame2" "data.frame3" "data.frame4"