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.

 

Example Data

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
 
# [[1]]
# x1 x2
#  1  a
#  2  b
#  3  c
 
# [[2]]
# 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 R programming codes of this page:

 

The YouTube video will be added soon.

 

Furthermore, you may want to read the other posts of my website.

 

In summary: In this R tutorial you learned how to add multiple data frames to a list. Let me know in the comments, in case you have further questions.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top