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).
To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.
The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause.
To create a list of Dataframes we use the list() function in R and then pass each of the data frame you have created as arguments to the function.
Rbind as is only accepts two dataframes, so we have to adjust our code slightly to accommodate for more dataframes.
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.frame
s you require.
l.df <- lapply(ls(pattern="df[0-9]+"), function(x) get(x))
Loads all data.frame
s in current environment that begins with df
followed by 1 to any amount of numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With