data_frame
s (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).