Convert a List to Vector in R (With Examples) - Data Science Parichay (2024)

Convert a List to Vector in R (With Examples) - Data Science Parichay (1)

/ Article, List, R / By Piyush Raj

In this tutorial, we will look at how to convert a list to a vector in R with the help of some examples.

How to convert a list to a vector in R?

You can use the built-in unlist() function in R to convert a list to a vector. The following is the syntax –

# convert list to vectorunlist(x, recursive=TRUE, use.names=TRUE)

Pass the list you want to convert as an argument to the unlist() function. Let’s look at the arguments for the unlist() function in more detail.

  • x – The R object (in our case, a list) that you want to convert.
  • recursive (optional) – It determines whether to unlist individual objects as well in x. It is TRUE by default.
  • use.names (optional) – This argument determines whether to preserve the original element names from x. It is TRUE by default.

Examples

Let’s better understand the usage of the unlist() function with the help of some examples.

List of numbers to a vector in R

First, we will convert a simple list of numbers to a vector using the unlist() function.

# create a listls <- list(1, 2, 3, 4)# convert list to vectorvec <- unlist(ls)# display the resulting vectorprint(vec)

Output:

[1] 1 2 3 4

Here, we pass the list of numbers to the unlist() function. You can see that we get a numeric vector as output.

List with mixed values to a vector in R

Remember that a list can store values of different data types together whereas in a vector the data type of the values has to be the same.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

What would happen if we convert a list with mixed values (values of different data types) to a vector? Let’s find out.

# create a listls <- list(1, FALSE, "cat")# convert list to vectorvec <- unlist(ls)# display the resulting vectorprint(vec)

Output:

[1] "1" "FALSE" "cat" 

Here, we create a list with a numeric, a logical, and a character type value and then use the unlist() function to convert it to a vector.

You can see that all the values in the resulting vector are of character type. This is because R performed implicit type conversion so that all the values have the same type. This is similar to creating a vector with mixed values using the c() function.

Convert List with named values to a Vector

The use.names parameter is TRUE by default for the unlist() function. This means that the names of values in a list will be preserved when converting to a vector by default. Let’s look at an example.

# create a list with named valuesls <- list(a=1, b=2, c=3)# convert list to vectorvec <- unlist(ls)# display the resulting vectorprint(vec)

Output:

a b c 1 2 3 

You can see that the names are preserved in the resulting vector.

If you, however, do not want the names to be preserved pass FALSE to the use.names parameter.

# create a list with named valuesls <- list(a=1, b=2, c=3)# convert list to vector and dont preserve the namesvec <- unlist(ls, use.names=FALSE)# display the resulting vectorprint(vec)

Output:

[1] 1 2 3

You can see that the names are not preserved in the resulting vector.

You might also be interested in –

  • How to Create a List in R?
  • How to Print a Vector in R?
  • Create a Vector in R – With Examples


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

  • Convert a List to Vector in R (With Examples) - Data Science Parichay (2)

    Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    View all posts

  • Convert a List to Vector in R (With Examples) - Data Science Parichay (3)

    Gottumukkala Sravan Kumar

    View all posts

Convert a List to Vector in R (With Examples) - Data Science Parichay (2024)
Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6060

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.