Convert List of Vectors to Data Frame in R (2 Examples)

 

This tutorial explains how to convert a list of vectors to a data frame in the R programming language.

Table of contents:

Let’s get started!

 

Creating Example Data

In the following examples, we will use this example list:

my_list <- list(A = 1:5, B = letters[1:5])   # Create example list
my_list
# $A
# [1] 1 2 3 4 5
# 
# $B
# [1] "a" "b" "c" "d" "e"

As you can see based on the output of the RStudio console, our example list contains two vectors. One of them is numeric and one of them is a character.

 

Example 1: Convert List to Data Frame Columns

If we want to convert each of the two list elements to a column, we can use a combinations of the cbind, do.call, and as.data.frame R functions:

as.data.frame(do.call(cbind, my_list))       # Convert list to data frame columns
#   A B
# 1 1 a
# 2 2 b
# 3 3 c
# 4 4 d
# 5 5 e

You can see based on the RStudio console output that each of our list elements was converted to a column.

Note that the list elements need to have the same length.

 

Example 2: Convert List to Data Frame Rows

We can also use the rbind function instead of cbind in order to convert our example list to a data frame with two rows and five columns (i.e. each value of our list elements is stored in a new column):

as.data.frame(do.call(rbind, my_list))       # Convert list to data frame rows
#   V1 V2 V3 V4 V5
# A  1  2  3  4  5
# B  a  b  c  d  e

 

Video, Further Resources & Summary

Some time ago I have released a video on my YouTube channel, which shows the topics of this article. You can find the video below:

 

 

In addition, you may want to read the related tutorials of https://statisticsglobe.com/. You can find a selection of tutorials about merging, binding and combining lists and data frames below:

 

In this post, I showed how to convert a list to a dataframe with column names in the R programming language. In case you have additional questions, let me know in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


10 Comments. Leave new

  • Hi Joachim,

    I was creating a data frame with a large file and it takes long long time to finish, I finally stopped it
    and wonder if there is another and maybe faster to generate a data frame
    below my command:
    for (i in 1:length(date)) {

    ps = tibble(lon,slp[,,i] %>% as_tibble())%>%
    as_tibble() %>%
    gather(key = “key”, value = “slp”, 2:242) %>%
    mutate(time = date[i], lat = rep(lat, each = 301)) %>% select(time, lon,lat, slp)

    slp.df = slp.df %>% bind_rows(ps)

    }

    Reply
    • Hey Abdoulaye,

      Thanks for commenting on my website! 🙂

      So your data is stored in a list called date?

      for-loops are always very slow and it is usually much faster to try to use functions instead. Have you tried to use the combination of do.call and cbind as shown in Example 1 of this tutorial?

      Regards,

      Joachim

      Reply
  • Thanks Joachim,

    Here I have gridded data (sea level pressusre) in netcdf files, I read it in R and trying to convert as a data frame. It is 3D lon lat time. So may data frame should look like this:

    time lon lat slp

    1 1978-12-30 12:00:00 0 40 1015.
    2 1978-12-30 12:00:00 2.5 40 1013.
    3 1978-12-30 12:00:00 5 40 1012.
    4 1978-12-30 12:00:00 7.5 40 1010.
    5 1978-12-30 12:00:00 10 40 1007.
    6 1978-12-30 12:00:00 12.5 40 1005.
    7 1978-12-30 12:00:00 15 40 1004.
    8 1978-12-30 12:00:00 17.5 40 1003.
    9 1978-12-30 12:00:00 20 40 1002.
    10 1978-12-30 12:00:00 22.5 40 1001.
    # … with 53,909,990 more rows

    I am not familiar with your do.call, but could see if I can introduce do.call in my script. Here date is the time

    Best regards,
    Abdoulaye

    Reply
  • Hi Joachim,

    > class(slp)
    [1] “array”

    Thanks

    Reply
    • OK this is a bit strange 😀

      What is returned if you execute slp[1]? 1 1978-12-30 12:00:00 0 40 1015.?

      If yes, could you try to run strsplit(slp[1], split = ” “) and tell me what is returned?

      Reply
  • Hi Joachim,

    There is an error:
    Error: unexpected numeric constant in “slp[1]? 1 1978”

    I am tacking too much of your time so I will leave it like this or maybe send you a small sample data for you to try, which is in netcdf format?

    Best regards,
    Abdoulaye

    Reply
    • Hey Abdoulaye,

      Yes, please send a small sample by email. I’ll have a look at it. Thanks for trying to explain it publicly in the comments section of my website!

      Regards,

      Joachim

      Reply
  • Hi Joachim,

    Thanks for your great articles.
    Brief and clear examples!.

    *** Quick Question ***:

    How to modify the R code in EX #1 (List $elements to DF cols),
    as.data.frame( do.call( cbind, my_list ) )

    IF:
    – the $A list element has 4 items and
    – the $B list element has 5 items ?.

    i.e.:
    > my_list my_list # see? different number of List items (4 and 5)
    $A
    [1] 1 2 3 4

    $B
    [1] “a” “b” “c” “d” “e”

    I guess,
    it would be OK
    if the Dataframe would contain an:
    NA character,
    in place of any missing element/s for that column…

    Joe
    San Francisco.
    ===========

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top