20

Is it possible to put a lot of data frames into a list in some easy way? Meaning instead of having to write each name manually like the following way:

list_of_df <- list(data_frame1,data_frame2,data_frame3, ....)

I have all the data frames loaded into my work space. I am going to use the list to loop over all the data frames (to perform the same operations on each data frame).

Paul Hiemstra
  • 56,833
  • 11
  • 132
  • 142
Martin Petri Bagger
  • 1,625
  • 4
  • 14
  • 16
  • @Imo and @EdChum; This question was posted before the question you marked it as a dublicate of. Please look at the dates! – Martin Petri Bagger Mar 02 '17 at 10:01
  • This duplicate tag seems questionable. The other question seems directed toward declaring a list of data frames. This question seems directed toward putting already declared data frames into a list. This is a slight nuance; however, these are slightly different tasks. – Agriculturist Apr 12 '18 at 19:04

3 Answers3

29

You can use ls() with get as follows:

l.df <- lapply(ls(), function(x) if (class(get(x)) == "data.frame") get(x))

This'll load all data.frames from your current environment workspace.

Alternatively, as @agstudy suggests, you can use pattern to load just the data.frames you require.

l.df <- lapply(ls(pattern="df[0-9]+"), function(x) get(x))

Loads all data.frames in current environment that begins with df followed by 1 to any amount of numbers.

Arun
  • 108,644
  • 21
  • 263
  • 366
  • 3
    Is there a way to keep the names of the objects included intact? This method works well but dataframe names are not the ones extracted. Is there a better way than assign them separately ? Thanks. – Anusha Jun 22 '13 at 05:37
  • @Anusha, Sorry I don't quite get what you mean. Are you saying that you want a list of data.frames with names of each element of the list being the names of those data.frames? – Arun Jun 22 '13 at 06:36
  • Yes. The objects have been identified by their names but are not being included in the list. I would prefer to assign the names of the dataframes selected in this step itself. – Anusha Jun 22 '13 at 12:06
  • Please let me know if this requires posting a separate question.I asked in a comment as it seems the solution might be a little modification of the code here. – Anusha Jun 22 '13 at 17:54
  • 3
    Just wrap the `lapply` with `setNames`: `setNames(lapply(ls(pattern="df[0-9]+"), function(x) get(x)), ls(pattern="df[0-9]+"))` – Arun Jun 23 '13 at 11:01
  • I was trying using setNames with glob2rx in the `pattern = " "` and its giving error that argument nm is missing with no default. It works for the above search pattern though.Thanks for the help. – Anusha Jun 23 '13 at 12:12
8

By far the easiest solution would be to put the data.frame's into a list where you create them. However, assuming you have a character list of object names:

list_df = lapply(list_object_names, get)

where you could construct you list like this (example for 10 objects):

list_object_names = sprintf("data_frame%s", 1:10)

or get all the objects in your current workspace into a list:

list_df = lapply(ls(), get)
names(list_df) = ls()
Paul Hiemstra
  • 56,833
  • 11
  • 132
  • 142
5

You can use ls with a specific pattern for example. For example:

some data.frames:

data.frame1 <- data.frame()
data.frame2 <- data.frame()
data.frame3 <- data.frame()
data.frame4 <- data.frame()

list(ls(pattern='data.fra*'))
[[1]]
[1] "data.frame1" "data.frame2" "data.frame3" "data.frame4"
agstudy
  • 113,354
  • 16
  • 180
  • 244