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

时间:2013-07-15 19:48:45

标签: 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 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

2 个答案:

答案 0 :(得分:5)

我认为你错误地写了subsets。如果它实际上是这样的:

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

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

并且customeridsc(1,1),然后您可以将subsets作为data.frame列中的列表,因为总行数仍然相同。你可以这样做:

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" 

现在,您可以像访问DF$value一样访问list并执行操作。

答案 1 :(得分:2)

改为使用data.table

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>