In this short tutorial, you will learn how to convert a list to a dataframe in R. To know how to convert lists to dataframes may be useful when you, for example, get your data from a source and they end up in a list of e.g. vectors. Here’s an example code template that you can use to change a list of vectors to a dataframe in R:

# Converting list to dataframe in R DF <- as.data.frame(YourList)
Code language: R (r)

In the next section, you will get an overview of the outline of this post. 

Outline

The outline of this post is as follows. First, you will get some information on what you need to follow this tutorial. First, you will get to create example data to use in the rest of the post. Third, we will have a look at the first example of converting a list to a dataframe in R. Fourth, you will learn how to convert a list to a dataframe by a second example. Now, we will also have a look at how we can use the do.call function. This can be done to make the rows columns. Here it may be worth pointing out that this can be done also using the t function. See the post “How to Transpose a Dataframe or Matrix in R with the t() Function” for more information. In the last example, you will learn how to use Tidyverse to change a list to a dataframe. 

Requirements

To follow this post, and all of its examples, you will need to have 1) a working R installation, 2) the dplyr package which is part of the Tidyverse package. On the other hand, if you only want to know how to create a dataframe from a list you can stick with base R. However, it is worth pointing out that dplyr can be used to add a column to the dataframe in R, remove duplicates, and count the number of occurrences in a column.

In the next example, we will create some sample data using some base R functions. 

Example Data

Here’s how we can create a list containing a couple of vectors:

Data <- list(A = seq(1,4), B = c("A", "D", "G", "L"), C = c("Best", "List", "Dataframe", "Rstats"))
Code language: R (r)

We can also display our list (and the vectors) like this:

Data
Code language: R (r)

In the two code chunks above, we used the list() function together with two functions to first create the list called Data. Here we used c and seq in R to generate vectors. Additionally, we named the different vectors, in the list, to A, B, and C. Finally, we printed the list using the name of the list (i.e., Data). Here’s the generated list:

In the next section, you will learn how to convert the list to a dataframe.

Example 1: Convert List to Dataframe in R

In the first example, we are going to use R’s as.data.frame() function to convert the list to a dataframe:

dataFrame <- as.data.frame(Data) dataFrame
Code language: R (r)

In the code chunk above, we simply used the above mentioned function using the list as the first argument. Here’s the dataframe that we generated from our list:

If your data is stored in a matrix, for example, it is possible to convert a matrix to a dataframe in R. In the next example, we are going to set the column names while converting the list to a dataframe.

Example 2: Convert a List to Dataframe in R and Changing the Column Names

Here’s how we simply add the col.names parameter to change the column names:

dataFrame <- as.data.frame(Data, col.names = c("Numbers", "Letters", "Words")) dataFrame
Code language: R (r)

Noteworthy, it is also possible to name the rows by using the row.names argument. However, if you have a lot of observations/data points this might not be feasible. In the next example, we are going to have a look at how we can use the do.call function to accomplish the same but making the rows columns as well. In a recent post, you can learn how to create a matrix in R.

Example 3: Converting a List to Dataframe in R using the do.call Function

In this example, you will learn how to use the as.data.frame function, the cbind, and the do.call functions to convert a list to a dataframe in R. Here’s a code snippet:

as.data.frame(do.call(cbind, Data))
Code language: R (r)

As you can see, in the code chunk above, we used the do.call function as an argument in the cas.data.frame function. Moreover, we used the cbind function and, finally, the list we wanted to convert as the last argument. This will create a dataframe similar to the earlier ones. Now, you may wonder why we would like to do something like this. Well, we can use the rbind function instead of the cbind function. This will give us this result:

As you can see, we got the rows as columns. If you need to change the column names you can have a look at the post: How to Rename Column (or Columns) in R with dplyr

In the next, and final example, we will use dplyr to convert a list to a dataframe in R.

Example 4: Convert List to Dataframe in R using dplyr

Here’s how we can convert a list to dataframe in R using dplyr:

library(dplyr) dataFrame <- Data %>% as_tibble() dataFrame
Code language: R (r)

In the code chunk above, there are some new things introduced. First, we used the piping operator (%>%). Following this operator we used the as_tibble function. This code chunk will create a dataframe called dataFrame by taking the list (Data) and put it as an argument to the as_tibble function. That is, we need to have the input/argument (i.e., the data in the list) left to the piping operator. 

Conclusion 

In this post, you have learned how to convert list to dataframe in R. More specifically, you learned how to do this by 4 examples. First, we started out using the as.data.frame function on an example list. Second, we changed the column names using one of the arguments of the as.data.frame function when we converted the list. Third, we also had a look on how we can use the do.call function. In the final example, we used the dplyr package from the popular Tidyverse package. To conclude, the easiest way to convert a list to a dataframe in R is to either use the as.data.frame function or the as_tibble function from dplyr. Hope you learned something valuable. If you did, please leave a comment below and share the posts on your social media accounts. Finally, if you want something covered on the blog – drop a comment below or use the contact information found here. 

  • Save
Share via
Copy link
Powered by Social Snap