创建一个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"