Add Key Value Pair to List in R (2 Examples)

 

This article explains how to append a key/value pair to a list object in R programming.

Table of contents:

Let’s take a look at some R codes in action:

 

Example Data

First, we have to create a vector containing our keys:

key <- c("key_1", "key_2", "key_3")    # Create key vector
key                                    # Print key vector
# [1] "key_1" "key_2" "key_3"

Next, we have to create another vector containing the corresponding values to our key variable:

value <- letters[1:3]                  # Create value vector
value                                  # Print value vector
# [1] "a" "b" "c"

Next, I’ll explain how to add these key/value combinations to a list object in R.

 

Example 1: Add Single Key/Value Pair to List

In Example 1, I’ll illustrate how to concatenate a single key/value pair to a list in R.

For this, we can use square brackets as shown below:

my_list1 <- list()                     # Create empty list
my_list1[key[1]] <- value[1]           # Add one key/value pair
my_list1                               # Print new list
# $key_1
# [1] "a"
#

Have a look at the previously shown output of the RStudio console: We have created a list with one list element.

The name of this list element is equal to our first key, and the content of this list element is equal to our first value.

 

Example 2: Add Multiple Key/Value Pairs to List Using for-Loop

In this example, I’ll illustrate how to bind multiple key/value pairs to a list using a for-loop.

Have a look at the R programming syntax below:

my_list2 <- list()                     # Create empty list
for(i in 1:length(key)) {              # Add key/value pairs in for-loop
  my_list2[key[i]] <- value[i]
}
my_list2                               # Print new list
# $key_1
# [1] "a"
# 
# $key_2
# [1] "b"
# 
# $key_3
# [1] "c"
#

The previous R code has constructed a list with three list elements. Each of these list elements corresponds to one of our key/value pairs.

 

Video & Further Resources

Do you need more explanations on the R programming code of this tutorial? Then you may want to have a look at the following video which I have published on my YouTube channel. In the video, I explain the R codes of this article:

 

The YouTube video will be added soon.

 

In addition to the video, you may have a look at the other articles of this website. I have released several articles on topics such as vectors, lists, and loops.

 

In this post you have learned how to add a key/value pair to lists in the R programming language. If you have additional questions, please let me know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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