如何在R中工作环境中的所有数据框架? [英] How to rbind all the data.frames in your working environment in R?
本文介绍了如何在R中工作环境中的所有数据框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Ex。我一直在做的:
df< - rbind(A,B,C,D,E,F)
我尝试过:
df< - rbind(ls())
但这只是创建一个在我工作环境中的所有data.frames的名称列表。
任何帮助将不胜感激。
解决方案
您可以搜索 data.frame
类的对象,使用函数 mget
来检索它们。
a = b = c = data.frame(x = 1:2,y = 3,z = 1:4)
d =junk
e = list(poo =pah)
ls()
#[1]abcde
dfs = sapply(.GlobalEnv,is.data.frame)
dfs
#abcde
#TRUE TRUE TRUE FALSE FALSE
do.call(rbind,mget(names(dfs)[dfs]))
#xyz
#a.1 1 3 1
#a.2 2 3 2
#a.3 1 3 3
#a.4 2 3 4
#b.1 1 3 1
#b.2 2 3 2
#b.3 1 3 3
#b.4 2 3 4
#c.1 1 3 1
#c.2 2 3 2
#c.3 1 3 3
#c.4 2 3 4
I have over 50 data.frames in my working environment that I would like to rbind. Is there a way to rbind the data.frames with out having to type out each date.frame?
Ex. of what I have been doing:
df <- rbind(A,B,C,D,E,F)
I have tried:
df <- rbind(ls())
But this just creates a list of names of all the data.frames in my working environment.
Any help would be appreciated.
解决方案
You can search for objects of data.frame
class, and use function mget
to retrieve them.
a = b = c = data.frame(x=1:2, y=3, z=1:4)
d = "junk"
e = list(poo="pah")
ls()
# [1] "a" "b" "c" "d" "e"
dfs = sapply(.GlobalEnv, is.data.frame)
dfs
# a b c d e
# TRUE TRUE TRUE FALSE FALSE
do.call(rbind, mget(names(dfs)[dfs]))
# x y z
# a.1 1 3 1
# a.2 2 3 2
# a.3 1 3 3
# a.4 2 3 4
# b.1 1 3 1
# b.2 2 3 2
# b.3 1 3 3
# b.4 2 3 4
# c.1 1 3 1
# c.2 2 3 2
# c.3 1 3 3
# c.4 2 3 4
这篇关于如何在R中工作环境中的所有数据框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文