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.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# 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.
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.