Convert large list to dataframe in R
Last Updated :
18 Jul, 2021
In this article, we will discuss how to convert a large list to a dataframe in the R Programming Language.
Method 1 : Using rbindlist()
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)
data <- matrix (data = 1, nrow = 300, ncol = 3)
list_data <- rep ( list (data), 18000)
list_data <- Map (as.data.frame, list_data)
datarbind <- rbindlist (list_data)
datarbind
|
Output :
fig : Large list to dataframe
Method 2: Using plyr package
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)
data <- matrix (data = 1, nrow = 300, ncol = 3)
list_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
.
.
.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...