East Liu
East Liu

Reputation: 107

automatic dataframe names

I have around 100 dataframes with same structure, like d1, d2 ,d3, ..., d10, d11, ..., d100. I have to rbind them together, like rbind(d1,d2,.....dxx).

I don't want to manually write all dataframes names because in that case I have manually write more than 100 dataframe names and that number could increase in future. can you please help write an automatic way to rbind(d1,d2,d3,...,d10, d11,.....,d100,....)?

Upvotes: 4

Views: 318

Answers (4)

Putapa
Putapa

Reputation: 1

Example - from my work:

filename_vector <- paste0(i, sep="_", df$unique.label.within.df, 
                          sep="", "intended.filename.csv")

Upvotes: -1

Roland
Roland

Reputation: 397

Building on akrun's elegant answer (using mget()), but using dplyr's efficient bind implementation to avoid the do.call():

library(dplyr)
mget(paste0("d",1:100)) %>% bind_rows()

Upvotes: 0

akrun
akrun

Reputation: 886948

We can use mget to return a list of values

out <- do.call(rbind, mget(paste0("d", 1:100)))

Upvotes: 2

Neeraj
Neeraj

Reputation: 1236

First create a character vector of all objects that you want to bind, like:

NameDf <- paste("d", 1:100, sep = "") 

Now, first call each object using get function and bind them together using do.call

NewDf <- do.call(cbind, lapply(NamesDf, FUN = function(x) get(x)))

Upvotes: 2

Related Questions