Data frame in R stores heterogeneous data types in the format of tabular forms. It is a list of vectors of equal length. Converting a list to a data frame provides more insight to analyze the tabular format of data.

The as.data.frame() function in R is used to create a data frame in R. It can create a data frame from the list of vectors by specifying the list parameter in data.frame() function.

In this tutorial, we will discuss how to convert a list to a data frame in R, using the do.call() ,cbind(), and rbind() function to convert the list of vectors to a data frame in R.

Convert List to Data Frame in R

To convert a list of vectors to a data frame in R, use below R code

dr <- as.data.frame(list)


Let’s practice with an example to make a data frame from the list in R.

name <- c("Tom", "Aron", "Gary","Jeannie")
gender <- c("Male", "Male", "Male", "Female")
age <- c(18, 20, 17, 21)

# Create a list from a vector
student_list <- list(
name = name,
gender = gender,
age = age
)
# Print the list of vectors
student_list


In the above R code, we have created the list from vectors.

The output of the list of vectors is:

$name [1] "Tom" "Aron" "Gary" "Jeannie"$gender
[1] "Male"   "Male"   "Male"   "Female"

\$age
[1] 18 20 17 21


Using the R as.data.frame() function to convert the list to a data frame.

# Convert the list to data frame
student_data = as.data.frame(student_list)
# Print the data frame
student_data


In the above R code, as.data.frame() function takes student_list as an argument and converts the list of vectors to a data frame.

The output of the above R code is:

     name gender age
1     Tom   Male  18
2    Aron   Male  20
3    Gary   Male  17
4 Jeannie Female  21


Converting the list to Data frame in R using do.call function

Using the do.call() and cbind() function in R, we can convert the list to a data frame. It converts the list elements to columns in a data frame.

Let’s use the above example of a list to convert the list to a data frame.

# Use do.call and cbind() function to convert list elements to data frame columns
student_data1 <- as.data.frame(do.call(cbind, student_list))
# Print the data frame
student_data1


The output of the above R code converts the list of vectors elements to data frame columns.

     name gender age
1     Tom   Male  18
2    Aron   Male  20
3    Gary   Male  17
4 Jeannie Female  21


Convert the List to Data Frame Rows using rbind function

Using the rbind() function in R, we can convert the list to data frame rows.

Let’s practice with an example to convert a list to data frame rows in R.

# Convert the list to data frame rows
student_data2 <- as.data.frame(do.call(rbind, student_list))
# Print the data frame
student_data2


In the above R code, as.data.frame() function accepts argument to convert the list to data frame rows using do.call() and rbind() function.

The output of the R code is:

         V1   V2   V3      V4
name    Tom Aron Gary Jeannie
gender Male Male Male  Female
age      18   20   17      21


Convert the List to Data frame using the dplyr package

The dplyr package is commonly used for data manipulation. It provides a consistent set of verbs to perform data manipulation.

Using the dplyr package, we can convert the list to a data frame.

# Import the library
library(dplyr)
# Convert the list of vectors to data frame usint dplyr
student_data3 <- student_list %>% as_tibble()
# Prints the data frame
student_data3


In the above R code, it pipe list using piping operator %>% to as_tibble() function.

The output of the above R code to convert the list to a data frame in r is:

name    gender   age
<chr>   <chr>  <dbl>
1 Tom     Male      18
2 Aron    Male      20
3 Gary    Male      17
4 Jeannie Female    21


Conclusion

I hope the above article on how to convert the list to a data frame in R is helpful to you.

We can use as.data.frame() function in R to convert the list of vectors to R. Other methods to convert the list to data frame columns is using do.call() function and cbind() function.

Using the do.call() and rbind() function, it can convert the list to data frame rows in R.

Use the dplyr package to convert the list to the data frame using the as_tibble() function.