4

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

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

>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
4

2 回答 2

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.

于 2013-07-15T20:14:43.680 回答
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>
于 2013-07-15T19:55:03.657 回答