How to Convert List to data frame in R

There are the following ways to convert a list to a data frame in R.

  1. Using as.data.frame() function
  2. Using List to a DataFrame using t()
  3. Convert Nested List to a DataFrame
  4. Convert Nested List to a DataFrame by Rows
  5. Use data.table
  6. Using tibble package
  7. Using dplyr package

Method 1: Using the as.data.frame() function

The as.data.frame() function checks if an object is a data frame; if not, it will try to convert it if possible.

Syntax

as.data.frame(x, row.names = NULL, optional = FALSE)

Parameters

  1. x: It is any R object.
  2. row.names:  It is NULL or a character vector giving the row names for the data frame. Missing values are not allowed.
  3. optional: It is logical. If TRUE, set row names and convert column names.

Example

app_list <- list(
 s1 = c(1, 3, 8),
 s2 = c(2, 4, 6),
 s3 = c(11, 21, 19))

app_list

cat("After converting list to data frame", "\n")
as.data.frame(app_list)

Output

$s1
[1] 1 3 8

$s2
[1] 2 4 6

$s3
[1] 11 21 19

After converting list to data frame
   s1 s2 s3
1  1  2  11
2  3  4  21
3  8  6  19

In this example, we first defined a list of three numeric vectors.

Then use the as.data.frame() method to convert the list to a data frame. The default for the parameter stringsAsFactors is now the default.stringsAsFactors() which yields FALSE as its default.

Method 2: Using the t() function

The t() function transposes a matrix or a data frame, but it can’t be directly used to convert a list to a data frame. However, you can first convert the list to a matrix and then transpose it, or you could convert the list to a data frame directly, depending on the structure of your list.

Example

# create a list
main_list <- list(
  name = c("Krunal", "Ankit", "Rushabh"),
  age = c(24, 27, 22),
  salary = c(60000, 70000, 65000)
)

# convert the list to a matrix
mat <- do.call(rbind, main_list)

# transpose the matrix
mat <- t(mat)

# convert the matrix to a data frame
df <- as.data.frame(mat)

print(df)

Output

   name    age  salary
1  Krunal  24   60000
2  Ankit   27   70000
3  Rushabh 22   65000

Method 3: Convert Nested List to a DataFrame

Converting a nested list (a list of lists) to a data frame in R can be accomplished using various techniques. The best method often depends on the structure of your list.

Example

# create a nested list
main_list <- list(
  list(name = "Krunal", age = 24, salary = 60000),
  list(name = "Ankit", age = 27, salary = 70000),
  list(name = "Rushabh", age = 22, salary = 65000)
)

# convert the nested list to a data frame
df <- do.call(rbind, lapply(main_list, function(x) as.data.frame(t(unlist(x)))))

print(df)

Output

   name   age   salary
1 Krunal  24    60000
2 Ankit   27    70000
3 Rushabh 22    65000

Method 4: Convert the Nested List to a DataFrame by Rows

To convert a nested list to a data frame by rows, one of the ways is using the purrr and dplyr packages from the tidyverse.

Example

# load necessary libraries
library(purrr)
library(dplyr)

# create a nested list
my_list <- list(
  list(name = "Krunal", age = 24, salary = 60000),
  list(name = "Ankit", age = 27, salary = 70000),
  list(name = "Rushabh", age = 22, salary = 65000)
)

# convert the nested list to a data frame
df <- map_df(my_list, ~ as.data.frame(t(.)))

print(df)

Output

  name    age  salary
1 Krunal  24   60000
2 Ankit   27   70000
3 Rushabh 22   65000

Method 5: Use data.table to Create DataFrame from List

The data.table package in R is a high-performance version of data.frame, especially for large datasets. It has several methods that allow you to perform operations like grouping and joining much faster than with base R methods.

Example

library(data.table)

# create a nested list
main_list <- list(
  list(name = "Krunal", age = 24, salary = 60000),
  list(name = "Ankit", age = 27, salary = 70000),
  list(name = "Charlie", age = 22, salary = 65000)
)

df <- rbindlist(main_list)

df

Output

    name    age   salary
1:  Krunal  24    60000
2:  Ankit   27    70000
3:  Charlie 22    65000

Method 6: Using tibble Package

The tibble is another package to create an R DataFrame from the list. The tibble package has a function enframe(), which takes nested list objects and converts them to nested tibble (“tidy” data frame) objects.

Example

library(tibble)

# create a nested list
main_list <- list(
  list(name = "Krunal", age = 24, salary = 60000),
  list(name = "Ankit", age = 27, salary = 70000),
  list(name = "Charlie", age = 22, salary = 65000)
)

df <- enframe(main_list)

df

Output

# A tibble: 3 × 2
     name   value
     <int> <list>
1      1  <named list [3]>
2      2  <named list [3]>
3      3 <named list [3]>

Method 7: Create DataFrame from List in R using plyr package

The plyr package in R provides a set of tools that makes it easier to manipulate data. The ldply() function from plyr can be used to convert a list into a data frame.

Example

library(plyr)

# create a nested list
main_list <- list(
  list(name = "Krunal", age = 24, salary = 60000),
  list(name = "Ankit", age = 27, salary = 70000),
  list(name = "Charlie", age = 22, salary = 65000)
)

df <- ldply(main_list, data.frame)

df

Output

   name    age   salary
1  Krunal  24    60000
2  Ankit   27    70000
3  Charlie 22    65000

That’s it.

Leave a Comment