Built with Tutify.io

    AIStudyLabs

    unlist() Function in R - Usage with Examples

    unlist() Function in R - Usage with Examples

    The unlist() function in R accepts a list as its parameter and converts it into a vector. In R, a list is a data structure that can hold elements of various data types, which makes it a heterogeneous data structure. On the other hand, a vector in R is a simpler data structure where all elements must be of the same type.

    A list can hold various data types such as characters, numerics, and complex types like data frames, vectors, and matrices. To convert these elements from the list to a vector, you can use the unlist() function.

    <a id="h-1-syntax-of-unlist-function"></a>

    1. Syntax of unlist() Function

    
    # Syntax of unlist()
    unlist(x, recursive = TRUE, use.names = TRUE)
    

    <a id="h-1-1-parameters-of-unlist"></a>

    1.1. Parameters of unlist()

    Following are the parameters of unlist() function.

    <a id="h-1-2-unlist-return-value-of-unlist"></a>

    1.2. unlist() Return value of unlist()

    unlist() function returns a vector with the elements from the list. When used use.names=FALSE it returns a vector without names.

    <a id="h-2-r-unlist-usage"></a>

    2. R unlist() Usage

    Use unlist() function to convert a list to a vector by unlisting the elements from a list. A list in R contains 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.

    Let's convert the simple list. first, create a list by using the list() function.

    
    # Create list
    li <- list('A','B','C')
    print(li)
    
    # unlist() usage
    v <- unlist(li)
    print(v)
    

    Yields below output.

    <a id="h-3-use-named-list"></a>

    3. Use Named List

    Most of the time when we have to use a list in R, we will use a nested list with names, also called a named list. Let's use the named list on unlist() function and validate the result. Here. I have created a list object with vector values.

    
    # Use unlist() on Named list
    li <- list(col1 = c(10, 20, 30),                
               col2 = c(1, 40, 3),                    
               col3 = c(30, 20))   
    print(li)
    
    v <- unlist(li)
    print(v)
    

    Yields below output. Note that using a named list, creates a vector with names.

    If you don't want names you can ignore them by using use.names=FALSE.

    <a id="h-4-unlisting-the-list-of-dataframe"></a>

    4. Unlisting the List of Dataframe

    As I explained in the beginning, a list in R can contain the data frame hence let's create a list with a dataframe element and convert it to a vector. While unlisting, if you want to return the dataframe column names, you can specify the use.names=TRUE. If you don’t want the column names, you can specify the use.names=FALSE. The following example uses the default option which is use.names=TRUE.

    Here, I have a list with a dataframe containing 3 columns col1,col2, and col3.

    
    # Convert List with data.frame
    li <- list(data.frame(col1 = c(10, 20, 30),                
               col2 = c(1, 40, 3),                    
               col3 = c(30, 20,4)) )
               
    print(li)
    v <- unlist(li,use.names=FALSE)
    print(v)
    

    Yields below output.

    <a id="h-5-complete-example-of-unlist-function-in-r"></a>

    5. Complete Example of unlist() Function in R

    Following is a complete example of using the unlist() function.

    
    # Create list
    li <- list('A','B','C')
    print(li)
    
    # unlist() usage
    v <- unlist(li)
    print(v)
    
    # Create Named list
    li <- list(col1 = c(10, 20, 30),                
               col2 = c(1, 40, 3),                    
               col3 = c(30, 20))   
    print(li)
    
    # Use unlist() on Named list
    v <- unlist(li)
    print(v)
    

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

    6. Conclusion

    In this article, you have learned unlist() function in R? unlist function syntax, and usage with different parameters. Finally learned how to change a list into a vector using this function with examples. 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