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)
# Creating the large list
data <- matrix(data = 1, nrow = 300, ncol = 3)
list_data <- rep(list(data), 18000)
# Mapping -> converting the list to
# dataframe
list_data <- Map(as.data.frame, list_data)
# Converting the large list to dataframe
# using the rbindlist function
datarbind <- rbindlist(list_data)
# Print the dataframe
datarbind
Output :
fig : Large list to dataframeMethod 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)
# Creating the large list
data <- matrix(data = 1, nrow = 300, ncol = 3)
# Replicating the list
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
.
.
.
Similar Reads
Convert list of lists to dataframe in R 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 co
2 min read
Convert Matrix to Dataframe in R In this article, we will discuss how to convert the matrix into DataFrame, or we can also say that we will discuss how to create a DataFrame from a matrix in R Programming Language. A matrix can be converted to a dataframe by using a function called as.data.frame(). It will take each column from the
1 min read
Convert dataframe column to list in R In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read
Convert Nested Lists to Dataframe in R In this article, we will discuss how to Convert Nested Lists to Dataframe in R Programming Language. It can be done with two methods: Convert Nested lists to Data Frame by Column.Convert Nested lists to Data Frame by Row. First, let's Create a nested list. Code block Output: fig 1: Nested List Metho
3 min read
Convert List of Vectors to DataFrame in R In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one
2 min read
Convert dataframe to list of vectors in R In this article, we will learn how to convert a dataframe into a list of vectors, such that we can use columns of the dataframes as vectors in the R Programming language. Dataframe columns as a list of vectors as.list() function in R Language is used to convert an object to a list. These objects can
3 min read