Create List of Vectors in R (Example)

 

On this page you’ll learn how to construct a list of vectors in the R programming language.

Table of contents:

Let’s do this.

 

Example: Create List of Vectors Using list() Function

The following R code illustrates how to create a list containing different vectors in R.

First, let’s create some example vectors:

vec1 <- 1:5                          # Create three example vectors
vec2 <- letters[1:7]
vec3 <- rep("x", 3)

The previous R code has created several vector objects, i.e. one numeric / integer vector and two character vectors.

Let’s combine these vectors in a list!

We can do that by specifying the names of our vectors within the list function:

my_list <- list(vec1, vec2, vec3)    # Create list of vectors
my_list                              # Print list of vectors
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "a" "b" "c" "d" "e" "f" "g"
# 
# [[3]]
# [1] "x" "x" "x"
#

Have a look at the previous output of the RStudio console. It shows that we have created a list object containing three vectors each as a separate list element.

 

Video, Further Resources & Summary

Do you need more explanations on the contents of this tutorial? Then you might have a look at the following video of my YouTube channel. I illustrate the R programming syntax of this tutorial in the video instruction:

 

 

In addition, you may want to read some of the related R programming tutorials of this website:

 

In summary: At this point you should have learned how to generate a list consisting of multiple vectors in R programming. Tell me about it in the comments section, if you have additional questions. Furthermore, please subscribe to my email newsletter to receive updates on the newest tutorials.

 

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