I am doing a series of intricate data manipulations and in doing so I create a series of dataframes from one "source" dataframe and dynamically name all my "subset" dataframes. They all have the same structure (columns) and I want to bind them together.
The challenge I have is that I can't seem to get the syntax for binding right after I dynamically name/create these dataframes.
So to create my "subset" dataframes I get the desired data into a dataframe called df_master
and name it using assign
. I do this inside of a for loop so I end up with 10 subset dataframes. Pseudo code looks like this:
for (i in 1:10){
.... do some stuff ...
master_df <- save into a df
assign(paste0("df_months_", i), df_master) # dynamically (re) name df_master
}
This works fine and I get my 10 dataframes names df_months_1
, df_months_2
, etc.
The trouble comes in when I want to bind. This post recommends binding multiple dataframes using do.call
. In order to do that I need to put my "subset" dataframes in a list and then use do.call
and rbind
. This is the part I cant get right. I think I need a list of the subset dataframes themselves. But I can't seem to create that list.
Per the linked solution I need:
new_df <- do.call("rbind", list(df_months_1, df_months_2, ...)
Not sure how to create that list given that I am dynamically creating names.
CodePudding user response:
As we have create multiple objects in the global env (not recommended), check those objects in the global env with ls
with a regex
as pattern
ls(pattern = "^df_months_\\d $")
It returns a vector
of object names that matches the pattern - df_months_
from the start (^
) of the string followed by one or more digits (\\d
) till the end ($
) of the string
Now, we get
the values of the objects. For >=1
objects, use mget
which returns a key/value pair as a named list
.
mget(ls(pattern = "^df_months_\\d $"))
Then, we use rbind
within do.call
to bind the elements of the list
do.call(rbind, mget(ls(pattern = "^df_months_\\d $")))