Convert large list to dataframe in R - GeeksforGeeks

18 Jul, 2021
In this article, we will discuss how to convert a large list to a dataframe in the R Programming Language. 
First, create a large list. Then use the Map function on the list and convert it to dataframe using the as.data.frame function in R. The map function applies a function and transforms the given input to each element of a list or vector. We will be using as.data.frame function inside the map to convert each element of a given list to a dataframe.
After mapping and converting each element of the list to a dataframe, the next step is to take the entire large list and convert it to a data table or dataframe using the rbindlist function in R.
Syntax : 
rbindlist( l, fill = FALSE, use.names = “check”, idcol = NULL)
Parameters :
l : This is a list of data.table or data.frame or list objects.
fill : This is false by default. If we specify this as true, then it automatically fills the missing columns with NAs.
use.names : By default, it is specified as check which implies all the elements may not have same names in the same order. If it is specified as true, then we bind by matching column names and if false, then we bind by matching positions.
idcol : It basically creates a column in the result, displaying which list item those particular rows came from.
You can also use system.time() function to calculate the elapsed time for conversion.
Example: Converting a large list to dataframe using rbindlist() method
R
library(Matrix)library(data.table)  # Creating the large listdata <- matrix(data = 1, nrow = 300, ncol = 3)list_data <- rep(list(data), 18000)  # Mapping -> converting the list to # dataframelist_data <- Map(as.data.frame, list_data)  # Converting the large list to dataframe# using the rbindlist functiondatarbind <- rbindlist(list_data)  # Print the dataframedatarbind
Output :
fig : Large list to dataframe
We will be using ldply() function of the ‘plyr‘ package. 
Syntax: ldply(large_list)
Parameter:
large_list: pass your large list
Import the required packages, then pass your list which you want to convert to a dataframe into ldply() function as an argument.
Example: Converting a large list to dataframe using plyr package
R
library(Matrix)library(plyr)  # Creating the large listdata <- matrix(data = 1, nrow = 300, ncol = 3)  # Replicating the listlist_data <- rep(list(data), 18000)  ldply(list_data)
Output:
  1 2 3
1     1 1 1
2     1 1 1
3     1 1 1
4     1 1 1
5     1 1 1
6     1 1 1
7     1 1 1
8     1 1 1
9     1 1 1
10    1 1 1
11    1 1 1
12    1 1 1
13    1 1 1
14    1 1 1
15    1 1 1
16    1 1 1
17    1 1 1
18    1 1 1
.
.
.
Picked
R DataFrame-Programs
R List-Programs
R-DataFrame
R-List
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R