Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

r - How to rbind all the data.frames in your working environment?

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?

Example 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...