list 创建一个 data.frame,其中一列是一个列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9547518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a data.frame where a column is a list
提问by flodel
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
?
另外,有没有办法df
在一次调用中创建(上面)data.frame
?
回答by Ben Bolker
Slightly obscurely, from ?data.frame
:
有点晦涩,来自?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').
如果将列表或数据框或矩阵传递给 'data.frame',就好像每个组件或列都作为单独的参数传递(除了类 '"model.matrix"' 的矩阵和受 'I ')。
So
所以
data.frame(a=1:3,b=I(list(1,1:2,1:3)))
seems to work.
似乎工作。
回答by mnel
If you are working with data.tables
, then you can avoid the call to I()
如果您正在使用data.tables
,那么您可以避免调用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
回答by Deleet
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
s(也称为tibbles
, tbl_df
, tbl
)本机支持使用data_frame
构造函数创建列表列。要使用它们,请使用它们加载众多库之一,例如tibble
,dplyr
或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:
它们实际上是data.frames
在引擎盖下,但有些修改。它们几乎总是可以正常使用data.frames
。我发现的唯一例外是,当人们进行不适当的类检查时,会导致问题:
> #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).
我建议在R 4 Data Science(免费)中阅读它们。