创建一个data.frame,其列将在每行中保存一个列表

创建一个data.frame,其列将在每行中保存一个列表,r,R,我无法创建包含由字符集合组成的列的数据框 不可能/我应该坚持列表吗 >subsets <- c(list("a","d","e"),list("a","b","c","e")) customerids <- c(1,1) transactions <- data.frame(customerid = customerids,subset =subsets) > str(transactions) 'data.frame': 2 obs. of 8 variab

我无法创建包含由字符集合组成的列的数据框

不可能/我应该坚持列表吗

>subsets <- c(list("a","d","e"),list("a","b","c","e"))
customerids <- c(1,1)
transactions <- data.frame(customerid = customerids,subset =subsets)
> str(transactions)
'data.frame':   2 obs. of  8 variables:
 $ customerid  : num  1 1
 $ subset..a.  : Factor w/ 1 level "a": 1 1
 $ subset..d.  : Factor w/ 1 level "d": 1 1
 $ subset..e.  : Factor w/ 1 level "e": 1 1
 $ subset..a..1: Factor w/ 1 level "a": 1 1
 $ subset..b.  : Factor w/ 1 level "b": 1 1
 $ subset..c.  : Factor w/ 1 level "c": 1 1
 $ subset..e..1: Factor w/ 1 level "e": 1 1

>子集使用
数据。表
代替:

library(data.table)

# note the extra list here
subsets <- list(list("a","d","e"),list("a","b","c","e"))
customerids <- c(1,1)

transactions <- data.table(customerid = customerids, subset = subsets)
str(transactions)
#Classes ‘data.table’ and 'data.frame':  2 obs. of  2 variables:
# $ customerid: num  1 1
# $ subset    :List of 2
#  ..$ :List of 3
#  .. ..$ : chr "a"
#  .. ..$ : chr "d"
#  .. ..$ : chr "e"
#  ..$ :List of 4
#  .. ..$ : chr "a"
#  .. ..$ : chr "b"
#  .. ..$ : chr "c"
#  .. ..$ : chr "e"
# - attr(*, ".internal.selfref")=<externalptr> 

transactions
#   customerid subset
#1:          1 <list>
#2:          1 <list>
库(data.table)
#注意这里的额外列表

子集我认为你把
子集写错了。如果事实上是这样:

subsets <- list(c("a", "d", "e"), c("a", "b", "c", "e"))
# [[1]]
# [1] "a" "d" "e"

# [[2]]
# [1] "a" "b" "c" "e"

现在,您可以访问
DF$value
并像在
列表上一样执行操作

数据帧意味着每列的行数相等。在这里,您的列表长度不等。@JamesPringle每个列表有2个元素column@nicolas,我认为您的
子集有误。检查我的答案。哦!我不知道这件事。但是
数据。表
由于这个原因与
POSIXlt
有问题吗?@asb我不知道与
POSIXlt
有关的任何问题。。。?
DF <- data.frame(id = customerids, value = I(subsets))
#   id      value
# 1  1    a, d, e
# 2  1 a, b, c, e

sapply(DF, class)
#        id     value 
# "numeric"    "AsIs"