You are currently viewing 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.

1. Syntax of unlist() Function


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

1.1. Parameters of unlist()

Following are the parameters of unlist() function.

  • x – Input R object which you wanted to unlist. It can be a list or vector.
  • recursive – Recursively apply unlisting elements of x. Accept values TRUE/FALSE.
  • use.names – Use FALE to ignore the names in the vector. Accept values TRUE/FALSE.

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.

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.

r unlist function

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.

unlist with names

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

unlist to vector

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.

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)

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.

References