相关疑难解决方法(0)

创建一个data.frame,其中列是列表

我知道如何添加列表列:

> df <- data.frame(a=1:3)
> df$b <- list(1:1, 1:2, 1:3)
> df
  a       b
1 1       1
2 2    1, 2
3 3 1, 2, 3
Run Code Online (Sandbox Code Playgroud)

这有效,但不是:

> df <- data.frame(a=1:3, b=list(1:1, 1:2, 1:3))
Error in data.frame(1L, 1:2, 1:3, check.names = FALSE, stringsAsFactors = TRUE) : 
  arguments imply differing number of rows: 1, 2, 3
Run Code Online (Sandbox Code Playgroud)

为什么?

另外,有没有办法df在一次调用中创建(上面)data.frame

r list dataframe

69
推荐指数
4
解决办法
3万
查看次数

ddply用于创建列表的并集

我有一个包含customerid和列表的数据框.我想合并那些与同一客户有关的清单.

library(plyr)
subsets <- list(c("a", "d", "e"), c("a", "b", "c", "e"))
customerids <- c(1,1)
transactions <- data.frame(customerid = customerids,subset =I(subsets))
> transactions
  customerid     subset
1          1    a, d, e
2          1 a, b, c, e
Run Code Online (Sandbox Code Playgroud)

如果我想将子集与ddply合并,我会得到一个扩展的结果

> ddply(transactions, .(customerid), summarise, subset=Reduce(union,subset))
  customerid subset
1          1   a
2          1   d
3          1   e
4          1   b
5          1   c
Run Code Online (Sandbox Code Playgroud)

虽然我希望所有的结果都在一排.

r plyr

7
推荐指数
1
解决办法
183
查看次数

标签 统计

r ×2

dataframe ×1

list ×1

plyr ×1