• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to convert a list to a dataframe

rstats101 · August 23, 2024 ·

In this tutorial, we will learn 3 different ways to convert a list object into a dataframe in R. First we will show how to use the base R function as.data.frame() to convert a list to a dataframe. Then we will show examples using map_df() function in purrr package in tidyverse and bind_rows() function in dplyr.

library(tidyverse)

First, let us create a small list object using list() function.

mylist <- list(
  x = letters[1:5],
  y = 3:7, 
  z = LETTERS[6:10]
) 

Our list looks like this

mylist

$x
[1] "a" "b" "c" "d" "e"

$y
[1] 3 4 5 6 7

$z
[1] "F" "G" "H" "I" "J"

Convert a list to a dataframe with as.data.frame()

as.data.frame(mylist)

  x y z
1 a 3 F
2 b 4 G
3 c 5 H
4 d 6 I
5 e 7 J

Convert a list to a dataframe using map_df in purrr

Here is example of using map_df() function in purrr to convert a list to a dataframe.

map_df(mylist, ~.x)

# A tibble: 5 × 3
  x         y z    
  <chr> <int> <chr>
1 a         3 F    
2 b         4 G    
3 c         5 H    
4 d         6 I    
5 e         7 J    

Convert a list to a dataframe with bind_rows

Here is example of using dplyr’s bind_rows() function to convert a list to a dataframe.

bind_rows(mylist)

# A tibble: 5 × 3
  x         y z    
  <chr> <int> <chr>
1 a         3 F    
2 b         4 G    
3 c         5 H    
4 d         6 I    
5 e         7 J    


Related

Filed Under: as.data.frame, dplyr bind_rows(), map_df, rstats101 Tagged With: convert list to dataframe

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version