Convert list of lists to dataframe in R
Last Updated :
30 May, 2021
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
lists = list (list1 = list (1, 2, 3, 4, 5),
list2 = list ( 'a' , 'b' , 'c' , 'd' , 'e' ),
list3 = list (2, 3, 4, 5, 6))
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
lists = list (list1 = list (1, 2, 3, 4, 5),
list2 = list ( 'a' , 'b' , 'c' , 'd' , 'e' ))
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
lists = list (list1 = list (1, 2, 3, 4, 5),
list2 = list ( 'a' , 'b' , 'c' , 'd' , 'e' ),
list3 = list (2, 3, 4, 5, 6))
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
Share your thoughts in the comments
Please Login to comment...