Categories
dataframe list r r-faq

How do I make a list of data frames?

248

How do I make a list of data frames and how do I access each of those data frames from the list?

For example, how can I put these data frames in a list ?

d1 <- data.frame(y1 = c(1, 2, 3),
                 y2 = c(4, 5, 6))
d2 <- data.frame(y1 = c(3, 2, 1),
                 y2 = c(6, 5, 4))

3

  • 17

    This is in a couple answers, but it’s worth having a visible comment here too: use = not <- inside data.frame(). By using <- you create y1 and y2 in your global environment and your data frame isn’t what you want it to be.

    Feb 20, 2015 at 17:21

  • 44

    Look at that mess of code with no spaces and <-s inside data.frame(). What a newb I was.

    – Ben

    Jul 24, 2015 at 14:47

  • 9

    Not anymore. I just edited your question to fix the code formatting. Feel free to revert if you feel nostalgic.

    Dec 11, 2017 at 1:32

151

This isn’t related to your question, but you want to use = and not <- within the function call. If you use <-, you’ll end up creating variables y1 and y2 in whatever environment you’re working in:

d1 <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))
y1
# [1] 1 2 3
y2
# [1] 4 5 6

This won’t have the seemingly desired effect of creating column names in the data frame:

d1
#   y1....c.1..2..3. y2....c.4..5..6.
# 1                1                4
# 2                2                5
# 3                3                6

The = operator, on the other hand, will associate your vectors with arguments to data.frame.

As for your question, making a list of data frames is easy:

d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))
d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
my.list <- list(d1, d2)

You access the data frames just like you would access any other list element:

my.list[[1]]
#   y1 y2
# 1  1  4
# 2  2  5
# 3  3  6

0

    151

    This isn’t related to your question, but you want to use = and not <- within the function call. If you use <-, you’ll end up creating variables y1 and y2 in whatever environment you’re working in:

    d1 <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))
    y1
    # [1] 1 2 3
    y2
    # [1] 4 5 6
    

    This won’t have the seemingly desired effect of creating column names in the data frame:

    d1
    #   y1....c.1..2..3. y2....c.4..5..6.
    # 1                1                4
    # 2                2                5
    # 3                3                6
    

    The = operator, on the other hand, will associate your vectors with arguments to data.frame.

    As for your question, making a list of data frames is easy:

    d1 <- data.frame(y1 = c(1, 2, 3), y2 = c(4, 5, 6))
    d2 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
    my.list <- list(d1, d2)
    

    You access the data frames just like you would access any other list element:

    my.list[[1]]
    #   y1 y2
    # 1  1  4
    # 2  2  5
    # 3  3  6
    

    0

      23

      You can also access specific columns and values in each list element with [ and [[. Here are a couple of examples. First, we can access only the first column of each data frame in the list with lapply(ldf, "[", 1), where 1 signifies the column number.

      ldf <- list(d1 = d1, d2 = d2)  ## create a named list of your data frames
      lapply(ldf, "[", 1)
      # $d1
      #   y1
      # 1  1
      # 2  2
      # 3  3
      #
      # $d2
      #   y1
      # 1  3
      # 2  2
      # 3  1
      

      Similarly, we can access the first value in the second column with

      lapply(ldf, "[", 1, 2)
      # $d1
      # [1] 4
      # 
      # $d2
      # [1] 6
      

      Then we can also access the column values directly, as a vector, with [[

      lapply(ldf, "[[", 1)
      # $d1
      # [1] 1 2 3
      #
      # $d2
      # [1] 3 2 1