How to Append Key/Value Pair to List Object in R (Example Code)

In this tutorial you’ll learn how to append a key/value pair to a list object in R.

Creating Example Data

my_list <- list()                  # Initialize empty list
my_key <- "this_is_my_key"         # Define key
my_key                             # Display key in RStudio console
# [1] "this_is_my_key"
my_value <- 99999                  # Define value
my_value                           # Display value in RStudio console
# [1] 99999

Example: Append Key/Value Pair to List Using Square Brackets

my_list[my_key] <- my_value        # Adding key/value pair to empty list
my_list                            # Display updated list in RStudio console
# $this_is_my_key
# [1] 99999
#

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.
You need to agree with the terms to proceed

Menu
Top