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
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 #  "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.
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:
- Create Nested List in R
- Create Data Frame with Spaces in Column Names
- Create List of Vectors in R
- Extract Names of List Elements
- List All Column Names But One in R
- R Programming Examples
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.
Statistics Globe Newsletter