Open In App

Convert list of lists to dataframe in R

Last Updated : 30 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to convert a list of lists to dataframe in R Programming Language. We will convert a list of lists to a data frame both by row and by column.

Example 1: R program to create three lists inside a list with numeric and character type and convert into dataframe by column.

Syntax as.data.frame(do.call(cbind, list_name))

Parameters: Where cbind is to convert list to dataframe by column and list_name is the input list which is list  of lists

Code:

R




# create list and create 3 lists 
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
             list2 = list('a', 'b', 'c', 'd', 'e'),
             list3 = list(2, 3, 4, 5, 6))
  
# convert list of lists into dataframe
# by column
print(as.data.frame(do.call(cbind, lists)))


Output:

  list1 list2 list3
1     1     a     2
2     2     b     3
3     3     c     4
4     4     d     5
5     5     e     6

Example 2: R program to create two lists inside a list with numeric and character type and convert into dataframe by column

R




# create list and create 2 lists 
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
             list2 = list('a', 'b', 'c', 'd', 'e'))
  
# convert list of lists into
# dataframe by column
print(as.data.frame(do.call(cbind, lists)))


Output:

  list1 list2
1     1     a
2     2     b
3     3     c
4     4     d
5     5     e

Example 3: R program to create three lists inside a list with numeric and character type and convert into dataframe by column.

Syntax: as.data.frame(do.call(rbind,list_name))

Parameters: Where rbind is to convert list to dataframe by row and list_name is the input list which is list  of lists

R




# create list and create 3 lists 
# inside this list
lists = list(list1 = list(1, 2, 3, 4, 5),
             list2 = list('a', 'b', 'c', 'd', 'e'),
             list3 = list(2, 3, 4, 5, 6))
  
# convert list of lists into dataframe 
# by row
print(as.data.frame(do.call(rbind, lists)))


Output:

      V1 V2 V3 V4 V5
list1  1  2  3  4  5
list2  a  b  c  d  e
list3  2  3  4  5  6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads