Create List of Data Frames in R (Example)
This tutorial explains how to make a list of data frames in the R programming language.
The table of content looks as follows:
Here’s how to do it.
For the example of this tutorial, I’ll use the following two data frames in R:
data1 <- data.frame(x1 = 1:3, # Create first example data frame x2 = letters[1:3]) data2 <- data.frame(y1 = c(5, 1, 5, 1, 5), # Create second example data frame y2 = rep("a", 5))
The first data frame is called data1 and contains the two columns x1 and x2; The second data frame is called data2 and contains the two columns y1 and y2.
Now, let’s concatenate these two exemplifying data frames to a list in R…
Example: How to Create a List of Data Frames
In order to create a list of data frames in R, we can use the list() function. Within the function, we simply have to specify all data frames that we want to include to the list separated by a comma.
Have a look at the following R code:
my_list <- list(data1, data2) # Create list of data frames my_list # Print list to RStudio console # [] # x1 x2 # 1 a # 2 b # 3 c # [] # y1 y2 # 5 a # 1 a # 5 a # 1 a # 5 a
As you can see based on the output of the RStudio console, the previous R syntax created a list object called my_list, which contains our two data frames.
Video & Further Resources
Have a look at the following video of my YouTube channel. In the video tutorial, I explain the how to save and store multiple data frames in a list using the R programming codes of this page:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may want to read the other posts of my website.
- Convert List of Vectors to Data Frame in R
- Merge Two Lists in R (Example)
- Merge Multiple Data Frames in List
- The R Programming Language
In summary: In this R tutorial you learned how to add multiple data frames to a list. Please let me know in the comments, in case you have further questions.
Statistics Globe Newsletter