Create List with Names but no Entries in R (Example)

 

This article illustrates how to construct a list with names but no entries in the R programming language.

Table of contents:

Let’s dig in…

 

Example: Create List with Empty Elements Using sapply() Function

This example shows how to use the sapply function to get a list with list names but without list entries.

As a first step, we have to create a vector of list names:

my_names <- LETTERS[1:5]                         # Create vector of names
my_names                                         # Print vector of names
# [1] "A" "B" "C" "D" "E"

As you can see in the RStudio console, we have created a vector of five elements. We’ll use this vector to define our list names in the next step:

my_list <- sapply(my_names, function(x) NULL)    # Create list with empty elements
my_list                                          # Print list with empty elements
# $A
# NULL
# 
# $B
# NULL
# 
# $C
# NULL
# 
# $D
# NULL
# 
# $E
# NULL

After executing the previous R syntax, we have created a list with names but without entries.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. I’m explaining the R syntax of this tutorial in the video:

 

 

In addition, you might want to have a look at the related articles on my homepage. Some tutorials that are related to the construction of a list with names but without entries are listed below:

 

In this R tutorial you have learned how to create a list with names but without entries. Please let me know in the comments section below, if you have any further questions or comments.

 

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top