Convert list to dataframe with specific column names in R

12 Dec, 2021
A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a unique name, which can be either equivalent to the lists’ components names or assigned explicitly. In this article, we will discuss how to convert a list to dataframe with specific column names in R Programming Language.
do.call() method in R constructs and executes a function call from a name or a function and a list of arguments to be passed during the function call. 
Syntax: do.call (fun , args)
Arguments : 
fun – The function name to be executed
args – list of arguments to the function call
The rbind() method is used as the fun during this function call, which binds the passed elements of the list as rows of the dataframe. The rows are named based on the corresponding components of the list. Therefore, the argument of the do.call() method is the list object. The column names can be modified using the colnames() method in R, which assigns the column names to the assigned vector. In case, the length of the column names vector is smaller, NA is assigned as the respective column name. The column names are preserved in the original dataframe object. The number of columns in the dataframe is equivalent to the size of each component in the list. 
do.call ( rbind , list)
The as.data.frame() method is used to map the object to a dataframe consisting of rows and columns. 
R
# declaring a list objectlst_obj <- list(row1 = 1 : 5,                row2 = LETTERS[1 : 5],                row3 = FALSE) print ("Original List")print (lst_obj) # binding columns togetherdf <- do.call(rbind, lst_obj)print ("Original dataframe") # converting to a dataframedata_frame <- as.data.frame(df)print (data_frame) print ("Modified dataframe")colnames(data_frame) <- c(  "ColA", "ColB", "ColC", "ColD", "ColE") print (data_frame)
Output:
[1] "Original List"
$col1
[1] 1 2 3 4 5

$col2
[1] "A" "B" "C" "D" "E"

$col3
[1] FALSE
[1] "Original dataframe"
        V1    V2    V3    V4    V5
row1     1     2     3     4     5
row2     A     B     C     D     E
row3 FALSE FALSE FALSE FALSE FALSE

[1] "Modified dataframe"
     ColA  ColB  ColC  ColD  ColE
row1     1     2     3     4     5
row2     A     B     C     D     E
row3 FALSE FALSE FALSE FALSE FALSE
Column names can also be assigned to the dataframe based on the naming of elements inside the list object. The naming is assigned even if any one of the vector components is assigned names. 
R
# declaring a list objectlst_obj <- list("Row 1" = c(col1 = 'a', col2 = 'b',                            col3 = 'c'),                "Row 2" = c(col1 = 'd', col2 = 'e',                            col3 = 'f')) print ("Original List")print (lst_obj) # binding columns togetherdf <- do.call(rbind, lst_obj)print ("dataframe") # converting to a dataframedata_frame <- as.data.frame(df)print (data_frame)
Output:
[1] "Original List"
$`Row 1`
col1 col2 col3
"a"  "b"  "c"

$`Row 2`
col1 col2 col3
"d"  "e"  "f"

[1] "dataframe"
     col1 col2 col3
Row 1    a    b    c
Row 2    d    e    f
It can be done using the cbind() and do.call() method.
do.call ( cbind , list)
The following properties are maintained : 
The names assigned to the components of the list become column names, which can be modified using the colnames() method.
The total number of rows is equivalent to the length of the components.
Row names are mapped to the row numbers.
Code:
R
# declaring a list objectlst_obj <- list(col1 = 1 : 5,                col2 = LETTERS[1 : 5],                col3 = FALSE) print ("Original List")print (lst_obj) # binding columns togetherdf <- do.call(cbind, lst_obj)print ("dataframe") # converting to a dataframeas.data.frame(df)
Output
[1] "Original List"
$col1
[1] 1 2 3 4 5

$col2
[1] "A" "B" "C" "D" "E"

$col3
[1] FALSE


[1] "dataframe"
 col1 col2  col3
1    1    A FALSE
2    2    B FALSE
3    3    C FALSE
4    4    D FALSE
5    5    E FALSE
adnanirshad158
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
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?