user1319449
user1319449

Reputation: 31

Rbinding a lot of data frames

I have a large number of data frames that I want to combine into one big data frame using rbind.

I have seen solutions here that do that, but they assume the data frames are stored in a list of data frames. Mine are not, and I don't know of a good way of getting them all into a list without doing as much work as rbinding them all one at a time. I can get a list of the data frame names using ls() but I can't seem to get a list of the data frames (as opposed to a list of the data frame names.)

Upvotes: 3

Views: 456

Answers (1)

tonytonov
tonytonov

Reputation: 25608

Assuming your data frames are named like df1, df2 and so on, you can use the following:

df1 <- data.frame(a=1:2, b=3:4)
df2 <- data.frame(a=0:1, b=3:4)
df3 <- data.frame(a=5, b=42)
# building a vector of names
df_names <- paste0('df', 1:3)
df_names
[1] "df1" "df2" "df3"
# getting a list of data frames
lapply(df_names, get)
[[1]]
  a b
1 1 3
2 2 4

[[2]]
  a b
1 0 3
2 1 4

[[3]]
  a  b
1 5 42
# binding data frames
do.call(rbind, lapply(df_names, get))
  a  b
1 1  3
2 2  4
3 0  3
4 1  4
5 5 42

Upvotes: 3

Related Questions