2013-07-15 52 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

相關問題