3

I have many data.tables in memory with names following a specific pattern (e.g.: RE_1, RE_2... CO_1, CO_2...). I want to bind them efficiently to get only two data.tables (RE and CO).

I tried:

RE <- rbindlist(ls(pattern = "RE"))

But I got the following error: "Error in rbindlist(ls(pattern = "RE")) : Input to rbindlist must be a list of data.tables".

Is there a way to make such a "usable" list of data.tables (or data frames)?

Henrik
  • 65,555
  • 14
  • 143
  • 159
user2165907
  • 1,401
  • 3
  • 13
  • 28
  • 3
    I think you should use the built in function `tables`, otherwise you will be picking everything that matches RE from the global environment, including lists, data frames and etc. In other words, the correct way of doing this is `temp <- tables(mb = FALSE, silent = TRUE)$NAME; rbindlist(mget(grep("RE", temp, value = TRUE))); rbindlist(mget(grep("CO", temp, value = TRUE)))` – David Arenburg May 21 '15 at 12:39
  • OK, nice! Maybe less intuitive than the @FlooO solution... But that's true I may have got some problems if I had other objects than data.tables following the same name pattern – user2165907 May 21 '15 at 14:49
  • 1
    It is less intuitive, but it will make sure you are extracting *only* `data.table`s from your environment. – David Arenburg May 21 '15 at 14:51
  • should mention that if you can avoid it, you should be creating these tables as a list in the first place--e.g., `rbindlist(lapply(file_names_RE,fread))` – MichaelChirico May 21 '15 at 19:09

1 Answers1

5

Try

rbindlist(lapply(ls(pattern = "RE"),get))

Dont know if this is the most effective way but... It works.

ls(...) returns a vector with the names of your data.tables. Not the data.tables themself. get gets you the object by name.

You can also use

rbindlist(mget(ls(pattern = "RE")))
Rentrop
  • 20,979
  • 10
  • 72
  • 100