R

R – Convert List to Data Frame

One day, a farmer asked his son to arrange the mangoes on a flat track onto a shelf. Being a student, he knows R programming. He compares the flat track as a list data structure and the shelf as a data frame. Then, he arranges the mangoes in a flat track onto a shelf and explains to his father about converting the vector to a data frame.

In this article, we will see how to convert a list to a data frame.

Let’s create a list with mango prices.

We can create a list using the list() function.

#create list with 8 mango prices

mango_price=list(100,230,222,671,45,67,44,33)

 

#return list

print(mango_price)

Result:

Approach 1: Using data.frame()

The data.frame() is used to create a data frame from the given input list.

Syntax:

data.frame(list_object)

Where,

list_object is the list

Example

In this example, we are converting a list of mango_price to the data frame:

#create list with 8 mango prices

mango_price=list(100,230,222,671,45,67,44,33)

 

#convert list to dataframe

print(data.frame(mango_price))

#get the type

print(str(data.frame(mango_price)))

Result:

We will return the data frame converted from the list and get the type using the str() function.

Method 1: Using data.frame() With rbind

Here, we are converting a list to a data frame such that the list elements are placed as a row using the rbind parameter. We can use this using the do.call() method.

It takes two parameters. The first parameter is the rbind and the second parameter is the input list object.

Syntax:

data.frame(do.call(rbind, list_object))

Parameters:

  1. rbind refers to the row binding.
  2. list_object is the list.

Example

In this example, we are converting a list to a data frame with rbind inside the do.call() method:

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by row

print(data.frame(do.call(rbind, mango_price)))

Result:

We can see that the nested lists of price and quantity are arranged row-wise in the data frame.

Method 3: Using data.frame() With cbind

Here, we are converting a list to a data frame such that the list elements are placed as a column using rbind parameter. We can use this using the do.call() method.

It takes two parameters. The first parameter is the cbind, and the second parameter is the input list object.

Syntax:

data.frame(do.call(cbind, list_object))

Parameters:

  1. cbind refers to the column binding.
  2. list_object is the list.

Example

In this example, we are converting a list to a data frame with cbind inside the do.call() method.

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by column

print(data.frame(do.call(cbind, mango_price)))

Result:

We can see that the nested lists of price and quantity are arranged column-wise in the data frame.

Method 4: Using data.frame() With sapply()

Here, we are converting a list to a data frame using sapply() that takes two parameters. The first parameter is the list object, and the second parameter is c, which refers to combine. Finally, this method is surrounded by t() to transpose this into a data frame.

Syntax:

data.frame(t(sapply(list_object,c)))

Parameters:

  1. list_object is the list.
  2. c refers to combine.

Example 1

In this example, we are converting a single list to a data frame.

#create list with 8 mango prices

mango_price=list(100,230,222,671,45,67,44,33)

 

#convert list to dataframe by row

print(data.frame(t(sapply(mango_price,c))))

Result:

We can see that the list is converted to a data frame.

Example 2

In this example, we are converting a nested list to a data frame.

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by row

print(data.frame(t(sapply(mango_price,c))))

Result:

We can see that the nested lists of price and quantity are converted to a data frame.

Approach 2: Using as.data.frame()

The data.frame() is used to convert the given input list to a data frame.

Syntax:

as.data.frame(list_object)

Where,

list_object is the list

Example

In this example, we are converting a list of mango_price to the data frame.

#create list with 8 mango prices

mango_price=list(100,230,222,671,45,67,44,33)

 

#convert list to dataframe

print(as.data.frame(mango_price))

 

#get the type

print(str(as.data.frame(mango_price)))

Result:

We will return the data frame converted from the list and get the type using the str() function.

Method 1: Using data.frame() With rbind

Here, we are converting a list to a data frame such that the list elements are placed as a row using rbind parameter. We can use this using the do.call() method.

It takes two parameters. The first parameter is the rbind and the second parameter is the input list object.

Syntax:

as.data.frame(do.call(rbind, list_object))

Parameters:

  1. rbind refers to the row binding
  2. list_object is the list

Example

In this example, we are converting a list to a data frame with rbind inside the do.call() method.

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by row

print(as.data.frame(do.call(rbind, mango_price)))

Result:

We can see that the nested lists price and quantity are arranged row-wise in the data frame.

Method 3: Using data.frame() With cbind

Here, we are converting a list to a data frame such that the list elements are placed as a column using rbind parameter. We can use this using the do.call() method.

It takes two parameters. The first parameter is the cbind and the second parameter is the input list object.

Syntax:

as.data.frame(do.call(cbind, list_object))

Parameters:

  1. cbind refers to the column binding.
  2. list_object is the list.

Example

In this example, we are converting a list to a data frame with cbind inside the do.call() method.

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by column

print(as.data.frame(do.call(cbind, mango_price)))

Result:

We can see that the nested lists of price and quantity are arranged column-wise in the data frame.

Method 4: Using data.frame() With sapply()

Here, we are converting a list to a data frame using sapply() that takes two parameters. The first parameter is the list object, and the second parameter is c, which refers to combine. Finally, this method is surrounded by t() to transpose this into a data frame.

Syntax:

as.data.frame(t(sapply(list_object,c)))

Parameters:

  1. list_object is the list.
  2. c refers to combine.

Example 1

In this example, we are converting a single list to a data frame.

#create list with 8 mango prices

mango_price=list(100,230,222,671,45,67,44,33)

 

#convert list to dataframe by row

print(as.data.frame(t(sapply(mango_price,c))))

Result:

We can see that the list is converted to a data frame.

Example 2

In this example, we are converting a nested list to a data frame.

#create list with 8 mango prices

mango_price=list(price=list(100,230,222,671,45,67,44,33),quantity=list(0,20,3,4,2,10,56,4))

 

#convert list to dataframe by row

print(as.data.frame(t(sapply(mango_price,c))))

Result:

We can see that the nested lists of price and quantity are converted to a data frame.

Conclusion

So, the son explained to his father about converting the list to the data frame using data.frame() and as.data.frame() approaches by considering methods like do.call() with rbind and cbind. Thus, the father came to know that rbind will place the list row-wise in a data frame, and cbind will place the list column-wise in a data frame. Finally, use the sapply() with t() method to convert a list object to a data frame.

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.