I am trying to use rbind
on them. But I need a list of all the dataframes
that are already in my global environment. How can I do it?
我试图在他们身上使用rbind。但是我需要一个已经存在于我的全局环境中的所有数据帧的列表。我该怎么做?
Code I used to import the 20 csv files in a directory. Basically, have to combine into a single dataframe.
代码我用来导入目录中的20个csv文件。基本上,必须组合成一个数据帧。
temp = list.files(pattern = "*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
6 个解决方案
#1
6
From your posted code, I would recommend you start a new R session, and read the files in again with the following code
从您发布的代码中,我建议您启动一个新的R会话,并使用以下代码再次读取文件
do.call(rbind, lapply(list.files(pattern = ".csv"), read.csv))
#2
16
This function should return a proper list with all the data.frames as elements
此函数应返回一个包含所有data.frames作为元素的正确列表
dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))
then you can rbind them with
然后你可以用它们来对抗它们
do.call(rbind, dfs)
Of course it's awfully silly to have a bunch of data.frames lying around that are so related that you want to rbind
them. It sounds like they probably should have been in a list in the first place.
当然,拥有一堆数据框架是非常愚蠢的,这些数据框架是如此相关以至于你想要它们。听起来他们可能应该首先列入清单。
I recommend you say away from assign()
, that's always a sign things are probably afoul. Try
我建议你远离assign(),这总是一个迹象,事情可能会发生冲突。尝试
temp <- list.files(pattern="*.csv")
dfs <- lapply(temp, read.csv)
that should return a list straight away.
应该立即返回一个列表。
#3
4
If you only have data.frames with the same number of columns and column names in you global environment, the following should work (non-data.frame object don't matter):
如果您在全局环境中只有具有相同列数和列名的data.frames,则以下内容应该有效(非data.frame对象无关紧要):
do.call(rbind, eapply(.GlobalEnv,function(x) if(is.data.frame(x)) x))
#4
4
This is a slight improvement on MentatOfDune's answer, which does not catch data.frames with multiple classes:
这是MentatOfDune的答案略有改进,它没有捕获具有多个类的data.frames:
ls()[grepl('data.frame', sapply(ls(), function(x) class(get(x))))]
#5
3
The ls
function lists all things in your environment. The get
function gets a variable with a given name. You can use the class
function to get the class of a variable.
ls函数列出了环境中的所有内容。 get函数获取具有给定名称的变量。您可以使用类函数来获取变量的类。
If you put them all together, you can do this:
如果你把它们放在一起,你可以这样做:
ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame']
which will return a character vector of the data.frames in the current environment.
这将返回当前环境中data.frames的字符向量。
#6
0
To improve MentatOfDune's answer (great username by the way):
要改进MentatOfDune的答案(顺便说一句,用户名是:)
ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))]
This also supports tibbles (created with dplyr
for example), because they contain multiple classes, where data.frame
is one of them.
这也支持tibbles(例如用dplyr创建),因为它们包含多个类,其中data.frame就是其中之一。
#1
6
From your posted code, I would recommend you start a new R session, and read the files in again with the following code
从您发布的代码中,我建议您启动一个新的R会话,并使用以下代码再次读取文件
do.call(rbind, lapply(list.files(pattern = ".csv"), read.csv))
#2
16
This function should return a proper list with all the data.frames as elements
此函数应返回一个包含所有data.frames作为元素的正确列表
dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))
then you can rbind them with
然后你可以用它们来对抗它们
do.call(rbind, dfs)
Of course it's awfully silly to have a bunch of data.frames lying around that are so related that you want to rbind
them. It sounds like they probably should have been in a list in the first place.
当然,拥有一堆数据框架是非常愚蠢的,这些数据框架是如此相关以至于你想要它们。听起来他们可能应该首先列入清单。
I recommend you say away from assign()
, that's always a sign things are probably afoul. Try
我建议你远离assign(),这总是一个迹象,事情可能会发生冲突。尝试
temp <- list.files(pattern="*.csv")
dfs <- lapply(temp, read.csv)
that should return a list straight away.
应该立即返回一个列表。
#3
4
If you only have data.frames with the same number of columns and column names in you global environment, the following should work (non-data.frame object don't matter):
如果您在全局环境中只有具有相同列数和列名的data.frames,则以下内容应该有效(非data.frame对象无关紧要):
do.call(rbind, eapply(.GlobalEnv,function(x) if(is.data.frame(x)) x))
#4
4
This is a slight improvement on MentatOfDune's answer, which does not catch data.frames with multiple classes:
这是MentatOfDune的答案略有改进,它没有捕获具有多个类的data.frames:
ls()[grepl('data.frame', sapply(ls(), function(x) class(get(x))))]
#5
3
The ls
function lists all things in your environment. The get
function gets a variable with a given name. You can use the class
function to get the class of a variable.
ls函数列出了环境中的所有内容。 get函数获取具有给定名称的变量。您可以使用类函数来获取变量的类。
If you put them all together, you can do this:
如果你把它们放在一起,你可以这样做:
ls()[sapply(ls(), function(x) class(get(x))) == 'data.frame']
which will return a character vector of the data.frames in the current environment.
这将返回当前环境中data.frames的字符向量。
#6
0
To improve MentatOfDune's answer (great username by the way):
要改进MentatOfDune的答案(顺便说一句,用户名是:)
ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))]
This also supports tibbles (created with dplyr
for example), because they contain multiple classes, where data.frame
is one of them.
这也支持tibbles(例如用dplyr创建),因为它们包含多个类,其中data.frame就是其中之一。