How to Add New Elements to a List in R (Example)
In this R programming tutorial you’ll learn how to append new elements to a list.
The post will contain the following topics:
Let’s take a look at some R codes in action:
Creation of Exemplifying Data
Let’s first create an example list for the example of this R programming tutorial:
my_list <- list(L1 = 1:5, L2 = "XXX") # Create example list my_list # Print example list # $L1 #  1 2 3 4 5 # # $L2 #  "XXX"
Our list contains two list elements called L1 and L2.
Let’s also create a data object that we can append to our list later on:
my_element <- LETTERS[1:3] # Create example element my_element # Print example element # "A" "B" "C"
Our example element is a vector consisting of three characters.
Example: Appending New List Element in R
Now, we can merge our example list with our example element. Have a look at the following R code:
my_list_new <- c(my_list, L3 = list(my_element)) # Add element to list my_list_new # Print updated list # $L1 #  1 2 3 4 5 # # $L2 #  "XXX" # # $L3 #  "A" "B" "C"
As you can see based on the output of the RStudio console, we have created a new list consisting of the list elements of our original list plus the character vector that was contained in our example data object. Note that the added list element was named L3.
Video & Further Resources
Do you need more information on the R code of this tutorial? Then you might watch the following video of my YouTube channel. I show the R programming code of this tutorial in the video.
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 might have a look at the other articles of www.statisticsglobe.com. I have released numerous other posts about the manipulation of lists already.
- How to Combine Lists in R
- Merge Two Lists in R
- Remove Element from List
- Add List Element in for-Loop
- Store Results of Loop in List
- Count Number of List Elements
- Test If List Element Exists
- Convert Data Frame Rows to List
- Create List of Data Frames
- The R Programming Language
In summary: You learned in this post how to create and name new list elements in R. Please let me know in the comments, if you have additional questions.