R

How to Use the Unlist() Function on a List

In some cases, you need to convert a list to a vector by unlisting the elements from a list.

Lists can have a list of elements or a dataframe.

In this R article, we will see how to unlist the list of elements with the unlist() function.

The unlist() function is used to convert the elements in a list to vector. We will discuss the different scenarios with the list elements to convert into a vector. 

Unlisting the List of Elements

In this approach, we unlist the list that has only elements. It takes the list object as a parameter.

Syntax

unlist(list_object)

Where, list_object is the input list with elements.

Example 1
In this case, we create a list of 5 mango prices and use the unlist() function to unlist the list.

#create list with 5 mango prices
mango_price=list(100,230,222,671,45)
 
#return list
print(mango_price)

#unlist the above list
unlisted=unlist(mango_price)

#display
print(unlisted)

Result

You can see that the list elements are unlisted to a vector.

Example 2
In this case, we create a list of 5 strings and use the unlist() function to unlist the list.

#create list with 5 mango strings
mango=list("type1","type2","type3","type4","type5")
 
#return list
print(mango)


#unlist the above list
unlisted=unlist(mango)

#display
print(unlisted)

Result

You can see that the list elements are unlisted to a vector.

Unlisting the List of Lists

In this approach, we unlist that list that contains a nested list (list of lists). After unlisting, if you want to return the list names, you can specify the use.names parameter to TRUE. If you don’t need the list names, you can specify the use.names parameter to FALSE. By default, it returns the list names.

Syntax

unlist(list_object,use.names)

Where list_object is the input list with lists and the use.names is used to return/remove the names from an unlisted list. 

Example 1

In this case, we create a list of 2 fruit prices that has 5 elements each and unlist them without names.

#create list with 2 list of fruit prices
price=list(mango=list(100,230,222,671,45),apple=list(12,63,45,33,32))
 
#return list
print(price)


#unlist the above list without list names
unlisted=unlist(price,use.names=FALSE)

#display
print(unlisted)

Result

You can see that the list elements are unlisted to a vector without the list names.

Example 2
In this case, we create a list of 2 fruit prices that has 5 elements each and unlist them with names.

#create list with 2 list of fruit prices
price=list(mango=list(100,230,222,671,45),apple=list(12,63,45,33,32))
 
#return list
print(price)


#unlist the above list with  list names
unlisted=unlist(price,use.names=TRUE)

#display
print(unlisted)

Result

You can see that the list elements are unlisted to a vector with the list names. 

Unlisting the List of Dataframe

In this approach, we unlist that list that contains a dataframe. After unlisting, if you want to return the dataframe column names, you can specify the use.names parameter to TRUE. If you don’t need the column names, you can specify the use.names parameter to FALSE. By default, it returns the column names.

Syntax

unlist(list_object,use.names)

Where the list_object is the input list with dataframe and the use.names is used to return/remove the names from an unlisted list. 

Example 1
In this case, we create a list that holds a single dataframe with 2 columns and unlist it without the column names.

#create a list that has one dataframe with 2 columns
mango_price=list(data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4')))

print(mango_price)

#unlist the list without column names
print(unlist(mango_price,use.name=FALSE))

Result

You can see that the dataframe inside the list is unlisted to a vector without the column names.

Example 2
In this case, we create a list that holds a single dataframe with 2 columns and unlist it with the column names.

#create a list that has one dataframe with 2 columns
mango_price=list(data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4')))

print(mango_price)

#unlist the list without column names
print(unlist(mango_price,use.name=TRUE))

Result

You can see that the dataframe inside the list is unlisted to a vector with the column names.

Conclusion

From this tutorial, we learned how to use the unlist() function on a list which is used to convert the list to a vector. We discussed the three different scenarios to unlist the list, nested list and list of dataframe to a vector. If you want to get the labels in the unlisted result, you can use the use.names parameter that is set to TRUE. Otherwise, you can set it to FALSE if you don’t want to return the labels.

About the author

Sireesha Lavu

This is Sireesha Lavu from Gogulamudi, Andhra Pradesh, India 522015.
I am currently working as a teacher and interested in writing technical articles on computer science.