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 vector unlist(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 isTRUE
by default.use.names
(optional) – This argument determines whether to preserve the original element names from x. It isTRUE
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 list ls <- list(1, 2, 3, 4) # convert list to vector vec <- unlist(ls) # display the resulting vector print(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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 list ls <- list(1, FALSE, "cat") # convert list to vector vec <- unlist(ls) # display the resulting vector print(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 values ls <- list(a=1, b=2, c=3) # convert list to vector vec <- unlist(ls) # display the resulting vector print(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 values ls <- list(a=1, b=2, c=3) # convert list to vector and dont preserve the names vec <- unlist(ls, use.names=FALSE) # display the resulting vector print(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 –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.