R – Convert List to Data Frame
To convert List to Data Frame in R, call as.data.frame() function and pass the list as argument to it.
In this tutorial, we will learn the syntax of as.data.frame() function, and how to create an R Data Frame from a List, or convert a given list of vectors to a Data Frame, with the help of examples.
Syntax
The syntax to create an R Data Frame from a List list
using as.data.frame() is
as.data.frame(x, row.names = NULL, optional = FALSE, ...,
cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE,
check.names = !optional,
stringsAsFactors = FALSE)
where x
is a list.
Each element of the list is converted to a column in the resulting Data Frame.
If the list has vectors as elements, the each vector would be transformed as a column in Data Frame. But, if the list is just a list of some primitive type values like numbers, strings, logical values, etc., then as.data.frame(x) would return a Data Frame with a single row, since each element of the list is converted to a column.
Examples
In the following program, we take a list of numbers, and convert this list to Data Frame.
example.R
list_1 <- list(41, 42, 43, 44, 45, 46)
df <- as.data.frame(list_1)
print(df)
Output
X41 X42 X43 X44 X45 X46
1 41 42 43 44 45 46
In the following program, we take a list of named vectors, list_1
and convert this list to Data Frame df
using as.data.frame() function. Then, we print this Data Frame to console.
list_1
contains named vectors. These vectors transform to columns in the data frame, and the names of these vector are transformed to column names of the data frame.
example.R
list_1 <- list(a = c(41, 42, 43), b = c(44, 45, 46))
df <- as.data.frame(list_1)
print(df)
Output
a b
1 41 44
2 42 45
3 43 46
If vectors in list_1 are not named vectors, then R automatically generates names for the columns.
example.R
list_1 <- list(c(41, 42, 43), c(44, 45, 46))
df <- as.data.frame(list_1)
print(df)
Output
c.41..42..43. c.44..45..46.
1 41 44
2 42 45
3 43 46
We may rename these column names. Refer Rename Column Names in R tutorial.
The vectors in list can contain values of any primitive types.
example.R
list_1 <- list(a = c(41, TRUE, 43), b = c("abc", 45, 'm'))
df <- as.data.frame(list_1)
print(df)
Output
a b
1 41 abc
2 1 45
3 43 m
The boolean value has been transformed to numeric value. TRUE has been transformed to 1. String and Character values remained as is.
The vectors in the list must be of same length. Otherwise, we would get an Error: “arguments imply differing number of rows”.
example.R
list_1 <- list(a = c(41, 42, 43), b = c(44, 45, 46, 47))
df <- as.data.frame(list_1)
print(df)
Output
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 3, 4
We can provide the column names via col.names argument.
example.R
list_1 <- list(c(41, 42, 43), c(44, 45, 46))
df <- as.data.frame(list_1, col.names = c("a", "b"))
print(df)
Output
a b
1 41 44
2 42 45
3 43 46
If the vectors in list are named vectors, then these would be overridden by the col.names
vector.
Conclusion
In this R Tutorial, we learned to convert a List to an R Data Frame using as.data.frame(), with the help of examples.