Convert list to dataframe with specific column names in R
Last Updated :
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.
Converting with list data in the form of rows
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 object
lst_obj <- list(row1 = 1 : 5,
row2 = LETTERS[1 : 5],
row3 = FALSE)
print ("Original List")
print (lst_obj)
# binding columns together
df <- do.call(rbind, lst_obj)
print ("Original dataframe")
# converting to a dataframe
data_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 object
lst_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 together
df <- do.call(rbind, lst_obj)
print ("dataframe")
# converting to a dataframe
data_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
Converting with list data in the form of columns
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 object
lst_obj <- list(col1 = 1 : 5,
col2 = LETTERS[1 : 5],
col3 = FALSE)
print ("Original List")
print (lst_obj)
# binding columns together
df <- do.call(cbind, lst_obj)
print ("dataframe")
# converting to a dataframe
as.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
Similar Reads
Convert DataFrame to Matrix with Column Names in R Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly
3 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
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
Create DataFrame with Spaces in Column Names in R In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language. Method 1: Using check.names attribute The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, wh
4 min read
Convert DataFrame Column to Numeric in R In this article, we are going to see how to convert DataFrame Column to Numeric in R Programming Language. All dataframe column is associated with a class which is an indicator of the data type to which the elements of that column belong to. Therefore, in order to simulate the data type conversion,
9 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 two columns of a data frame to a named vector in R In this article, we will discuss how to convert two columns of a dataframe to a named vector in R Programming Language. Let see the implementation stepwise: Example 1: Creating dataframe and convert columns to vector. Step 1: Here we create a DataFrame with name data. There is a total of two columns
2 min read
How to add suffix to column names in R DataFrame ? Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read
Convert dataframe rows and columns to vector in R In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met
2 min read
Extract specific column from a DataFrame using column name in R In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language. In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is,
5 min read