0

My problem is the following. Suppose I have 1000 dataframes in R with the names eq1.1, eq1.2, ..., eq1.1000. I would like a single dataframe containing my 1000 dataframes. Normally, if I have only two dataframes, say eq1.1 and eq1.2 then I could define

df <- data.frame(eq1.1,eq1.2)

and I'm good. However, I can't follow this procedure because I have 1000 dataframes.

I was able to define a list containing the names of my 1000 dataframes using the code

names <- c()
for (i in 1:1000){names[i]<- paste0("eq1.",i)} 

However, the elements of my list are recognized as strings and not as the dataframes that I previously defined.

Any help is appreciated!

antonioACR1
  • 1,303
  • 2
  • 15
  • 28
  • 2
    Where do these data frames come from in the first instance and why are they not in a list? To be explicit: *do not* generate separate data.frames. Generate a list of them and rbind/cbind/whatever them, afterwards. – Konrad Rudolph Jan 27 '17 at 15:16
  • `nam <- paste0("eq1.", 1:1000)` – jogo Jan 27 '17 at 15:18
  • 1
    Take a look at gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for reasons in storing data.frames in a list. – lmo Jan 27 '17 at 15:21

2 Answers2

1

How about

df.names <- ls(pattern = "^eq1\\.\\d")
eq1.dat <- do.call(cbind,
                   lapply(df.names,
                          get))
rm(list = df.names)
Ista
  • 10,139
  • 2
  • 37
  • 38
0
library(stringi)
library(dplyr)

# recreate dummy data

lapply(1:1000,function(i){
      assign(sprintf("eq1.%s",i),
             as.data.frame(matrix(ncol = 12, nrow = 13, sample(1:15))),
             envir = .GlobalEnv)
    })

# Now have 1000 data frames in my working environment named eq1.[1:1000]

> str(ls(pattern = "eq1.\\d+"))
> chr [1:1000] "eq1.1" "eq1.10" "eq1.100" "eq1.1000" "eq1.101" "eq1.102" "eq1.103" ...

1) create a holding data frame from the ep1.1 data frame that will be appended each iteration in the following loop

empty_df <- eq1.1

2) im going to search for all the data frame named by convention and create a data frame from the returned characters which represent our data frame objects, but are nothing more than a character string.

3) mutate that data frame to hold an indexing column so that I can order the data frames properly from 1:1000 as the character representation will not be in numeric order from the step above

4) Drop the indexing column once the data frame names are in proper sequence and then unlist the dfs column back into a character sequence and slice the first value out, since it is stored already to our empty_df

5) loop through that sequence and for each iteration globally assign and bind the preceding data frame into place. So for example on iteration 1, the empty_df is now the same as data.frame(ep1.1, ep1.2) and for the second iteration the empty_df is the same as data.frame(ep1.1, ep1.2, ep1.3)

NOTE: the get function takes the character representation and calls the data object from it. see ?get for details

lapply(
  data.frame(dfs = ls(pattern = 'eq1\\.\\d+'))%>% 
    mutate(nth = as.numeric(stri_extract_last_regex(dfs,'\\d+'))) %>%
    arrange(nth) %>% select(-nth) %>% slice(-1) %>% .$dfs, function(i){
      empty_df <<- data.frame(empty_df, get(i)) 
    }
)

All done, all the dataframes are bound to the empty_df and to check

 > dim(empty_df)
    [1]    13 12000
Carl Boneri
  • 2,632
  • 1
  • 13
  • 15