2013-07-15 127 views
2

我无法创建一个由一组字符组成的列的数据框。创建一个data.frame,其列将在每一行中保存一个列表

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

>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 
+0

数据帧意味着每列有相同数量的行。在这里,你的名单长度不等。 –

+1

@JamesPringle每列有2个元素 – nicolas

+0

@nicolas,我想你已经弄错了你的子集。检查我的答案。 – Arun

回答

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

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> 
+0

哦!我不知道这一点。但是由于这个原因''data.table''没有'POSIXlt'的问题吗? – asb

+0

@asb我不知道有什么问题'POSIXlt' ...? – eddi

相关问题