105

I know how to add a list column:

> df <- data.frame(a=1:3)
> df$b <- list(1:1, 1:2, 1:3)
> df
  a       b
1 1       1
2 2    1, 2
3 3 1, 2, 3

This works, but not:

> df <- data.frame(a=1:3, b=list(1:1, 1:2, 1:3))
Error in data.frame(1L, 1:2, 1:3, check.names = FALSE, stringsAsFactors = TRUE) : 
  arguments imply differing number of rows: 1, 2, 3

Why?

Also, is there a way to create df (above) in a single call to data.frame?

5 Answers 5

130

Slightly obscurely, from ?data.frame:

If a list or data frame or matrix is passed to ‘data.frame’ it is as if each component or column had been passed as a separate argument (except for matrices of class ‘"model.matrix"’ and those protected by ‘I’).

(emphasis added).

So

data.frame(a=1:3,b=I(list(1,1:2,1:3)))

seems to work.

5
  • 20
    For those interested, "I" means "Inhibit Interperetation/Conversion of objects". It creates an identical object but with "AsIs" appended to the set of classes. The "AsIs" class is really there just to be read by the data.frame() and formula() functions. Learn more here.
    – pwilcox
    Commented May 1, 2017 at 16:49
  • 2
    amazing, thanks for the solution. though just I for Inhibit Interperetation/Conversion of objects seems a bit too short :)
    – sertsedat
    Commented Nov 7, 2018 at 11:08
  • 1
    @pwilcox very interesting. Is there some method to call on an object to understand if it's protected by I? I guess it's class() ? e.g. I(iris) -> i; i %>% class() 3 [1] "AsIs" "data.frame" (returns AsIs class)
    – stevec
    Commented Apr 25, 2020 at 12:34
  • 2
    @stevec See GKi's recent answer, it's IMO much better.
    – jay.sf
    Commented Apr 10, 2022 at 10:37
  • Could be worth adding that you can also assign a list column after the data frame creation: df$b <- list(1,1:2, 1:3).
    – LMc
    Commented Jan 18 at 17:47
41

If you are working with data.tables, then you can avoid the call to I()

library(data.table)
# the following works as intended
data.table(a=1:3,b=list(1,1:2,1:3))

   a     b
1: 1     1
2: 2   1,2
3: 3 1,2,3
1
  • 3
    This is an underappreciated feature of data.table by a wide margin Commented Feb 5, 2018 at 19:18
28

data_frames (variously called tibbles, tbl_df, tbl) natively support the creation of list columns using the data_frame constructor. To use them, load one of the many libraries with them such as tibble, dplyr or tidyverse.

> data_frame(abc = letters[1:3], lst = list(1:3, 1:3, 1:3))
# A tibble: 3 × 2
    abc       lst
  <chr>    <list>
1     a <int [3]>
2     b <int [3]>
3     c <int [3]>

They are actually data.frames under the hood, but somewhat modified. They can almost always be used as normal data.frames. The only exception I've found is that when people do inappropriate class checks, they cause problems:

> #no problem
> data.frame(x = 1:3, y = 1:3) %>% class
[1] "data.frame"
> data.frame(x = 1:3, y = 1:3) %>% class == "data.frame"
[1] TRUE
> #uh oh
> data_frame(x = 1:3, y = 1:3) %>% class
[1] "tbl_df"     "tbl"        "data.frame"
> data_frame(x = 1:3, y = 1:3) %>% class == "data.frame"
[1] FALSE FALSE  TRUE
> #dont use if with improper testing!
> if(data_frame(x = 1:3, y = 1:3) %>% class == "data.frame") "something"
Warning message:
In if (data_frame(x = 1:3, y = 1:3) %>% class == "data.frame") "something" :
  the condition has length > 1 and only the first element will be used
> #proper
> data_frame(x = 1:3, y = 1:3) %>% inherits("data.frame")
[1] TRUE

I recommending reading about them in R 4 Data Science (free).

2
  • 1
    R is moving and growing and I think this is the 2018 answer to the question and somehow it should be marked as such. Commented Oct 30, 2018 at 9:41
  • 10
    If it is popular enough it will rise to the top. A lot of us still use base R ...
    – Ben Bolker
    Commented Nov 7, 2020 at 20:14
7

You can use list2DF to create a data.frame where a column is a list.

x <- list2DF(list(a=1:3, b=list(1:1, 1:2, 1:3)))
#x <- data.frame(a=1:3, list2DF(list(b=list(1:1, 1:2, 1:3)))) #Alternative

x
#  a       b
#1 1       1
#2 2    1, 2
#3 3 1, 2, 3

str(x)
#'data.frame':   3 obs. of  2 variables:
# $ a: int  1 2 3
# $ b:List of 3
#  ..$ : int 1
#  ..$ : int  1 2
#  ..$ : int  1 2 3

With this you don't have the attr AsIs in the data.frame, what you would have when using I.

1

tibble >= 1.1.0

As of July 2016, tibble() and as_tibble() became preferred over data_frame() and as_data_frame() within the tidyverse.

library(tibble)

tibble(a = 1:3, b = list(1:1, 1:2, 1:3))

# create tibble row-wise
tribble(~ a, ~ b,
        1, list(1:1),
        2, list(1:2),
        3, list(1:3))

These functions are imported by many popular tidyverse packages such as dplyr.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.