Convert Nested Lists to Dataframe in R
Last Updated :
15 Nov, 2021
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
Method 1: To convert nested list to Data Frame by column.
Approach:
- Create dataframe using data.frame function with the do.call and cbind.
- cbind is used to bind the lists together by column into data frame.
- do.call is used to bind the cbind and the nested list together as a single argument in the Data frame function.
- Also, store the whole data frame in a variable named data_frame and print the variable.
Code:
R
# list() functions are used to create
# the list and those list() functions
# are put in another list() function to
# make the nested list
nested_list <- list(l1 = list(1:5, 5:1 ),
l2 = list(100:105, 105:100 ),
l3 = list(200:205, 205:200 ))
# Convert nested list to data frame
# by column with the help of cbind and do.call
data_frame <- as.data.frame(do.call(cbind, nested_list))
# Print data frame
data_frame
Output:
l1 l2 l3
1 1, 2, 3, 4, 5 100, 101, 102, 103, 104, 105 200, 201, 202, 203, 204, 205
2 5, 4, 3, 2, 1 105, 104, 103, 102, 101, 100 205, 204, 203, 202, 201, 200
Method 2: To convert nested list to Data Frame by row.
Approach:
- Create dataframe using data.frame function with the do.call and rbind.
- rbind is used to bind the lists together by row into data frame.
- do.call is used to bind the rbind and the nested list together as a single argument in the Data frame function.
- Also, store the whole data frame in a variable named data_frame and print the variable.
Code:
R
# list() functions are used to create
# the list and those list() functions
# are put in another list() function to
# make the nested list
nested_list <- list(l1 = list(1:5, 5:1 ),
l2 = list(100:105, 105:100 ),
l3 = list(200:205, 205:200 ))
# print the nested list
nested_list
# Convert nested list to data frame by row
# with the help of rbind and do.call
data_frame <- as.data.frame(do.call(rbind, nested_list))
# Print data frame
data_frame
Output:
V1 V2
l1 1, 2, 3, 4, 5 5, 4, 3, 2, 1
l2 100, 101, 102, 103, 104, 105 105, 104, 103, 102, 101, 100
l3 200, 201, 202, 203, 204, 205 205, 204, 203, 202, 201, 200
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 large list to dataframe in R 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 fun
3 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 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
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