You are currently viewing How to Convert List to Vector in R

To convert a list to a vector in R use unlist() function. This function takes a list as one of the arguments and returns a Vector. A list in R is an object consisting of heterogeneous elements meaning can contain elements of different types whereas a vector in R is a basic data structure containing elements of the same data type.

In this example, first I will create a list and convert it to a vector by using unlist() function. After converting to vector, you can use typeof() function to check its data type.

Quick Examples of Convert List to Vector

Following are quick examples of converting a list to a vector or creating a list from the vector.


# Quick Examples

# Convert list to vector
li <- list('A','B','C')
v <- unlist(li)
print(v)
print(typeof(v))

# Convert List of vectors into vector
li <- list(col1 = c(10, 20, 30),                
           col2 = c(1, 40, 3),                    
           col3 = c(30, 20))   
print(li)
v <- unlist(li, use.names=FALSE)
print(v)

# Convert to numeric vector
li <- list('10','20','30')
v <- unlist(li)
v <- as.vector(v,'numeric')
print(v)
typeof(v)

1. Create a List

Use the list() function to create a list object in R. A list in R is an object consisting of heterogeneous elements meaning can contain elements of different types. So the list can contain vectors, dataframe, matrix e.t.c


# Create list
li <- list('A','B','C')

li2 <- list(10,20,30)

li3 <- list('A','B','C',10,20,30)

2. Convert List to Vector

Use unlist() function to convert a list to a vector in R. Let’s pass the above created list object li as an argument to unlist() function, this returns a vector with each element of the list.


# Convert list to vector
v <- unlist(li)
print(v)

Yields below output. Since we have character elements in a list, it is converted into a character vector.


# Output
> print(v)
[1] "A" "B" "C"

Let’s check the data type of the converted list.


# Convert list to vector
typeof(v)

This outputs a type character.

3. Convert Named List to Vector

Let’s create a list with a named vector of numeric elements and convert it to a single vector.


# Convert List of vectors into vector
li <- list(col1 = c(10, 20, 30),                
           col2 = c(1, 40, 3),                    
           col3 = c(30, 20)) 
print(li)

#  Convert it to vector
v <- unlist(li)
print(v)

Yields below output. Notice that when you have a list with multiple vectors and unlisting will create a single vector flattened with all elements.


# Output
$col1
[1] 10 20 30

$col2
[1]  1 40  3

$col3
[1] 30 20

# Output of converted vector
col11 col12 col13 col21 col22 col23 col31 col32 
   10    20    30     1    40     3    30    20 

Use unlist(li, use.names=FALSE) to ignore names on the vector.


# Output
[1] 10 20 30  1 40  3 30 20

4. Convert List to Numeric Vector of type double

If you have a vector with numeric elements but as a string, you can use as.vector() function to convert to numeric type after converting list to vector.


li <- list('10','20','30')
v <- unlist(li)
v <- as.vector(v,'numeric')
print(v)
typeof(v)


# Output
[1] 10 20 30
[1] "double"

5. Convert Multiple Lists into Vector

To convert multiple lists use the c() function to combine the lists and pass this list to unlist() to convert it to a vector.


# Create Multiple Lists into Vector
li1 <- list('A','B','C')
li2 <- list('X','Y','Z')
v <- unlist(c(li1,li2))
print(v)

Yields below output.


# Output
[1] "A" "B" "C" "X" "Y" "Z"

Conclusion

In this article, you have learned how to convert a list to a vector in R by using unlist() function and also learned how to ignore the names on the vector by using use.names=FALSE param. You can find the complete example from this article at Github R Programming Examples Project.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium