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:

 

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

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.

 

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.

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

Menu
Top