Put multiple data frames into list (smart way)

27,470

Solution 1

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.

Solution 2

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

Solution 3

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"
Share:
27,470

Related videos on Youtube

Martin Petri Bagger
Author by

Martin Petri Bagger

Updated on July 09, 2022

Comments

  • Martin Petri Bagger
    Martin Petri Bagger almost 2 years

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

    • Martin Petri Bagger
      Martin Petri Bagger about 7 years
      @Imo and @EdChum; This question was posted before the question you marked it as a dublicate of. Please look at the dates!
    • Agriculturist
      Agriculturist about 6 years
      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.
  • Paul Hiemstra
    Paul Hiemstra about 11 years
    That gives you a list of names, not the content of the object.
  • Anusha
    Anusha almost 11 years
    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.
  • Arun
    Arun almost 11 years
    @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?
  • Anusha
    Anusha almost 11 years
    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
    Anusha almost 11 years
    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.
  • Arun
    Arun almost 11 years
    Just wrap the lapply with setNames: setNames(lapply(ls(pattern="df[0-9]+"), function(x) get(x)), ls(pattern="df[0-9]+"))
  • Anusha
    Anusha almost 11 years
    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.