Convert List to Vector in R (2 Examples)


This article explains how to convert a list to a vector object in the R programming language.

The article consists of this content:

Let’s just jump right in:


Creation of Example Data

At the start, we’ll have to create some data that we can use in the following examples:

my_list <- list(L1 = 5,                           # Create example list
                L2 = 8,
                L3 = 1,
                L4 = 5,
                L5 = 3)
my_list                                           # Print example list
# $L1
# [1] 5
# $L2
# [1] 8
# $L3
# [1] 1
# $L4
# [1] 5
# $L5
# [1] 3

The previous output of the RStudio console shows that our example list contains five list elements with list names.


Example 1: Converting List to Vector Using unlist() Function

The following R syntax illustrates how to apply the unlist function to convert our list to a vector object.

Have a look at the following R code and its output:

my_vector1 <- unlist(my_list)                     # Apply unlist function
my_vector1                                        # Print vector
# L1 L2 L3 L4 L5 
#  5  8  1  5  3

You can see based on the output in the RStudio console that we have converted our list to a named vector object.

Would you like to get rid of the names? Then keep on reading!


Example 2: Converting List to Vector without Names Using unlist() Function & use.names Argument

The R programming syntax below shows how to extract only the values when converting a list to a vector object.

For this, we have to set the use.names argument within the unlist function to be equal to FALSE:

my_vector2 <- unlist(my_list, use.names = FALSE)  # Apply unlist & use.names
my_vector2                                        # Print vector
# [1] 5 8 1 5 3

As you can see we have converted our list to an unnamed vector containing only of the numbers that are contained in our list.


Video & Further Resources

In case you need more information on the R programming code of this tutorial, I recommend having a look at the following video of my YouTube channel. In the video, I’m explaining the contents of this page.


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.


Furthermore, you may read the related articles on this homepage.


Summary: This article has shown how to convert lists to vectors in R programming. In case you have additional questions, let me know in the 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. Required fields are marked *

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