2
votes

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)?

1
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
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
It is less intuitive, but it will make sure you are extracting only data.tables from your environment. - David Arenburg
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

1 Answers

4
votes

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")))