Built with Tutify.io

    AIStudyLabs

    How to Convert List to Vector in R

    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.

    <a id="h-quick-examples-of-convert-list-to-vector"></a>

    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)
    

    <a id="h-1-create-a-list"></a>

    1. Create a List

    Use the list() function to create a list object in R. A list is an object capable of holding heterogeneous elements, allowing it to contain items of various types. Consequently, a list can include vectors, dataframe, matrix, and other types of elements.

    
    # Create list
    li <- list('A','B','C')
    
    li2 <- list(10,20,30)
    
    li3 <- list('A','B','C',10,20,30)
    

    <a id="h-2-convert-list-to-vector"></a>

    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.

    <a id="h-3-convert-named-list-to-vector"></a>

    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
    

    <a id="h-4-convert-list-to-numeric-vector-of-type-double"></a>

    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"
    

    <a id="h-5-convert-multiple-lists-into-vector"></a>

    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"
    

    <a id="h-conclusion"></a>

    Conclusion

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

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References