Convert List to DataFrame in R

Let’s see how to convert a list and nested list to R DataFrame in other words create a data frame in R from list. A list is a one-dimensional data structure that can store multiple data-type elements. In this article, I will explain how to convert a list and nested list to R DataFrame.

You can create a list in R by using the list() function. Use data.frame() method to convert any data structure to the list. And also if we want to convert nested lists to dataframe, we can use do.call() functions column and row-wise. as.data.frame() is also used to convert nested lists to a dataframe

Steps to Covert List to DataFrame

  • Step 1 – Create a List in R
  • Step 2 – Convert a List to DataFrame

1. Quick Examples of Convert List to R DataFrame

If you are in a hurry, below are quick examples of how to convert a list to DataFrame or create a DataFrame from the list.


# Create list with 5 elements
my_input_list = list(56,78,90,45,67)

# Convert dataframe to list using data.frame()
df <- data.frame(my_input_list)

# Convert dataframe to list using data.frame()
df <- data.frame(t(sapply(my_input_list,c)))

# Create nested list (3 lists inside)
my_nested_list = list(id=list(1,2,3,4,5),
   name=list('sai','ram','hari','deepak','sahithi'),
   gender=list('m','m','m','m','f'))

# Convert nested list to the dataframe by columns
df <-  as.data.frame(do.call(cbind, my_nested_list))

# Convert nested list to the dataframe by rows
df <- as.data.frame(do.call(rbind, my_nested_list))

# Using data.table
library(data.table)
df <- rbindlist(my_nested_list)

# Using tibble
library (tibble)
df <- enframe(my_nested_list)

# Using dplyr package
library(dplyr)
df <- as_tibble(my_nested_list)

In order to convert a list to R DataFrame, first, let’s create a list in R using list() function. This function takes the comma-separated values and returns a list type.

Syntax:


# List creation syntax
list(elements)

Where elements can be a vector, array, matrix, or single elements.

Let’s create a list with 5 elements and display them.


# Create list with 5 elements
my_input_list = list(56,78,90,45,67)

# Display the list
print(my_input_list)

Output:


# Output
[[1]]
[1] 56

[[2]]
[1] 78

[[3]]
[1] 90

[[4]]
[1] 45

[[5]]
[1] 67

Now, let’s see how to convert the list to an R DataFrame.

2. Convert List to DataFrame using data.frame()

data.frame() is used to create a DataFrame in R that takes a list, vector, array, etc as arguments, Hence, we can pass a created list to the data.frame() function to convert list to DataFrame. It will store the elements in a single row in the DataFrame. In the converted DataFrame, the column names will be X followed by the element from the list.

Syntax:


# Syntax create DataaFrame from list
data.frame(my_input_list)

Let’s convert the above created list to the DataFrame.


# Convert dataframe to list using data.frame()
df <- data.frame(my_input_list)
df

Yields below output.


# Output
  X56 X78 X90 X45 X67
1  56  78  90  45  67

Notice that the DataFrame is created from the list. This DataFrame contains 1 row with the number of columns equal to the number of elements in an array.

3. Convert List to a DataFrame using t()

t() stands for transpose. We can transpose a list to a DataFrame such that t() will take a parameter sapply().

sapply() take two parameters. The first parameter is the list name and the second is the c, which stands for the combine. Again, in the converted DataFrame, the column names will be X characters followed by the element count.

Syntax:


# Syntax
data.frame(t(sapply(my_input_list,c)))

Let’s convert the above created list to the DataFrame.


# Convert dataframe to list using data.frame()
df <- data.frame(t(sapply(my_input_list,c)))
df

Yields below output.


# Output
  X1 X2 X3 X4 X5
1 56 78 90 45 67

4. Convert Nested List to a DataFrame

as.data.frame() converts the nested list to an R DataFrame by taking do.call() function as a parameter. So each list inside a nested list will be a column in a DataFrame. So the column names in the DataFrame will be nested list names.

Syntax:


# Syntax using as.data.frame()
as.data.frame(do.call(cbind, my_nested_list))

Where do.call() takes two parameters.

Parameters:

  1. cbind stands for column binding that places elements column-wise in the converted DataFrame
  2. my_nested_list is the input nested list

Example:

Let’s create a nested list (3 lists inside) and convert it to the DataFrame.


#Create nested list (3 lists inside)
my_nested_list <- list(id=list(1,2,3,4,5),
    name=list('sai','ram','hari','deepak','sahithi'),
    gender=list('m','m','m','m','f'))

# Convert nested list to the dataframe by columns
df <- as.data.frame(do.call(cbind, my_nested_list))
df

Yields below output.


# Output
  id    name gender
1  1     sai      m
2  2     ram      m
3  3    hari      m
4  4  deepak      m
5  5 sahithi      f

We can see that the nested list is converted to a DataFrame as column values.

5. Convert Nested List to a DataFrame by Rows

as.data.frame() converts the nested list to a DataFrame by taking do.call() function as a parameter. So each list inside a nested list will be a row in a DataFrame. So the row names in the DataFrame will be nested list names. More examples of creating a Data Frame refer to different ways to create a Data Frame in R.

Syntax:


# Syntax
as.data.frame(do.call(rbind, my_nested_list))

Where do.call() takes two parameters.

Parameters:

  1. rbind stands for row binding that places elements row-wise in the converted DataFrame.
  2. my_nested_list is the input nested list.

Example:

Let’s create a nested list (3 lists inside) and convert it to the R DataFrame.


#Create nested list (3 lists inside)
my_nested_list = list(id=list(1,2,3,4,5),
    name=list('sai','ram','hari','deepak','sahithi'),
    gender=list('m','m','m','m','f'))

# Convert nested list to the dataframe by rows
df <- as.data.frame(do.call(rbind, my_nested_list))

Yields below output.


# Output
        V1  V2   V3     V4      V5
id       1   2    3      4       5
name   sai ram hari deepak sahithi
gender   m   m    m      m       f

We can see that the nested list is converted to a DataFrame as row values.

6. Use data.table to Create DataFrame from List

The package data.table has the function rbindlist which is a superfast implementation. This returns a data.table and to convert to data.frame use as.data.frame(DT). rbindlist() function. This also takes list of listsdata.frames or data.tables as input.


# Using data.table
library(data.table)
df <- rbindlist(my_nested_list)
df

Yields below output.


# Output
    V1  V2   V3     V4      V5
1:   1   2    3      4       5
2: sai ram hari deepak sahithi
3:   m   m    m      m       f

7. Using tibble Package

tibble is another package you can use to create a R DataFrame from list. The tibble package has a function enframe() which takes nested list objects and convert it to nested tibble (“tidy” data frame) objects.


# Using tibble
library (tibble)
df <- enframe(my_nested_list)
df

Yields below output.


# A tibble: 3 × 2
  name   value     
  <chr>  <list>    
1 id     <list [5]>
2 name   <list [5]>
3 gender <list [5]>

8. Create DataFrame from List in R using dplyr

In the following example, create a DataFrame from the list using the R dplyr package. dplyr is a third-party package hence, you need to load the library using library("dplyr") to use its methods. In case you don’t have this package, install it using install.packages("dplyr").

For bigger data sets it is best to use the methods from dplyr package as they perform 30% faster. dplyr package uses C++ code to evaluate.


# Using dplyr package
library(dplyr)
df <- as_tibble(my_nested_list)
df

9. Use plyr Package to create DataFrame from List

Use ldply() from plyr package to create and returns a data.frame from list. All the data types (character, numeric, etc) are correctly transformed. If the list has different data types they will be all transformed to character with matrix approach. 


# Using plyr package
library (plyr)
df <- ldply(my_nested_list, data.frame)
df

7. Conclusion

From this article, you have learned data.frame() and as.data.frame() can be used to convert a list to R DataFrame or create a data frame from a list. If you want the elements in the list column-wise, then use cbind otherwise you can use rbind. Also learned to use different third-party packages to create DataFrame from a list.

Related Articles

References

  1. R dataframe
  2. R List
r convert list dataframe

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Convert List to DataFrame in R