Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put multiple data frames into list (smart way) [duplicate]

Tags:

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

like image 713
Martin Petri Bagger Avatar asked Feb 19 '13 09:02

Martin Petri Bagger


People also ask

How do I combine Dataframes into a list in R?

To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.

How do I combine multiple data frames into one?

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.

How do I create a list of data frames in R?

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.

Can you Rbind more than 2 Dataframes?

Rbind as is only accepts two dataframes, so we have to adjust our code slightly to accommodate for more dataframes.


1 Answers

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.

like image 59
Arun Avatar answered Oct 10 '22 05:10

Arun